Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions api/src/main/java/org/commonjava/indy/conf/EnvironmentConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.conf;

import org.commonjava.web.config.ConfigurationException;
import org.commonjava.web.config.annotation.SectionName;
import org.commonjava.web.config.section.MapSectionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import java.io.File;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
* Created by yma on 2019/3/19.
*/

@ApplicationScoped
@SectionName( EnvironmentConfig.SECTION_NAME )
public class EnvironmentConfig
extends MapSectionListener
implements IndyConfigInfo
{
public static final String SECTION_NAME = "environment";

public static final String ENV_PREFIX = "mdc.env";

public static final String HOSTNAME = "HOSTNAME";

public static final String UNKNOWN = "UNKNOWN";

private final Logger logger = LoggerFactory.getLogger( getClass() );

private Map<String, String> envars = new HashMap<>();

public EnvironmentConfig()
{
}

@Override
public String getDefaultConfigFileName()
{
return new File( IndyConfigInfo.CONF_INCLUDES_DIR, "environment.conf" ).getPath();
}

@Override
public InputStream getDefaultConfig()
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream( "default-environment.conf" );
}

@Override
public synchronized void parameter( final String name, final String value )
throws ConfigurationException
{
if ( name.startsWith( ENV_PREFIX ) )
{
String envKey = name.substring( ENV_PREFIX.length() );
String enValue = System.getenv( envKey );
if ( envKey.equals( HOSTNAME ) && enValue == null )
{
try
{
enValue = InetAddress.getLocalHost().getHostName();
}
catch ( UnknownHostException e )
{
logger.error( String.format( "Unknown host. Reason: %s", e.getMessage() ), e );
}
}
envars.put( value, enValue == null ? UNKNOWN : enValue );
}
}

public Map<String, String> getEnvars()
{
return envars;
}
}
5 changes: 5 additions & 0 deletions core/src/main/conf/conf.d/environment.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[environment]
mdc.env.OPENSHIFT_BUILD_NAMESPACE = NAMESPACE
mdc.env.OPENSHIFT_BUILD_NAME = BUILD_NAME
mdc.env.OPENSHIFT_BUILD_COMMIT = BUILD_COMMIT
mdc.env.HOSTNAME = HOSTNAME
5 changes: 5 additions & 0 deletions core/src/main/resources/default-environment.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[environment]
mdc.env.OPENSHIFT_BUILD_NAMESPACE = NAMESPACE
mdc.env.OPENSHIFT_BUILD_NAME = BUILD_NAME
mdc.env.OPENSHIFT_BUILD_COMMIT = BUILD_COMMIT
mdc.env.HOSTNAME = HOSTNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.core.conf;

import org.commonjava.indy.conf.EnvironmentConfig;
import org.commonjava.indy.test.utils.WeldJUnit4Runner;
import org.commonjava.web.config.ConfigUtils;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

/**
* Created by yma on 2019/3/21.
*/
@RunWith( WeldJUnit4Runner.class )
public class EnvironmentConfigTest
{
@Inject
private Instance<EnvironmentConfig> instance;

@Test
public void weldInjection_IterateIndyLoggingConfigurators()
{
List<String> sections = new ArrayList<>();
instance.iterator().forEachRemaining( ( instance)->{
String section = ConfigUtils.getSectionName( instance );
System.out.printf( "Got instance: %s with section: %s\n", instance, section );
sections.add( section );
} );

System.out.println(sections);
assertThat( sections.contains( EnvironmentConfig.SECTION_NAME ), equalTo( true ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.bind.jaxrs;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.contrib.json.classic.JsonLayout;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.commonjava.indy.conf.EnvironmentConfig;
import org.commonjava.indy.model.core.io.IndyObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.inject.spi.CDI;
import java.util.Map;

import static org.commonjava.indy.bind.jaxrs.RequestContextConstants.ENVIRONMENT;

/**
* Created by yma on 2019/3/26.
*/
public class CustomJsonLayout
extends JsonLayout
{
private final Logger logger = LoggerFactory.getLogger( getClass() );

@Override
protected void addCustomDataToJsonMap( Map<String, Object> map, ILoggingEvent iLoggingEvent )
{
IndyObjectMapper objectMapper = new IndyObjectMapper( true );
super.addCustomDataToJsonMap( map, iLoggingEvent );

if ( !iLoggingEvent.getMDCPropertyMap().isEmpty() )
{
Map<String, String> mdcs = (Map<String, String>) map.get( MDC_ATTR_NAME );
try
{
Map<String, String> envars = CDI.current().select( EnvironmentConfig.class ).get().getEnvars();
mdcs.put( ENVIRONMENT, objectMapper.writeValueAsString( envars ) );
}
catch ( JsonProcessingException e )
{
mdcs.put( ENVIRONMENT, "{error: \"Envars could not be processed by Jackson.\"}" );
logger.error( String.format( "Failed to create environment mdc. Reason: %s", e.getMessage() ), e );
}
map.put( MDC_ATTR_NAME, mdcs );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package org.commonjava.indy.bind.jaxrs;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.commonjava.indy.conf.IndyConfiguration;
import org.commonjava.indy.conf.EnvironmentConfig;
import org.commonjava.indy.model.core.io.IndyObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ public class RequestContextConstants
@Thread @MDC
public static final String PREFERRED_ID = "preferred-id";

@Thread @MDC
public static final String ENVIRONMENT = "environment";
}