org.qi4j.library.fileconfig.FileConfigurationService Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.qi4j.library.fileconfig;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.qi4j.api.activation.ActivatorAdapter;
import org.qi4j.api.activation.Activators;
import org.qi4j.api.injection.scope.Structure;
import org.qi4j.api.injection.scope.This;
import org.qi4j.api.injection.scope.Uses;
import org.qi4j.api.mixin.Mixins;
import org.qi4j.api.property.Property;
import org.qi4j.api.service.ServiceComposite;
import org.qi4j.api.service.ServiceDescriptor;
import org.qi4j.api.service.ServiceReference;
import org.qi4j.api.structure.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Mixins( FileConfigurationService.Mixin.class )
@Activators( FileConfigurationService.Activator.class )
public interface FileConfigurationService
extends FileConfiguration, ServiceComposite
{
void resolveFileConfiguration();
public static class Activator
extends ActivatorAdapter>
{
@Override
public void afterActivation( ServiceReference activated )
throws Exception
{
activated.get().resolveFileConfiguration();
}
}
interface Data
{
Property os();
Property application();
Property user();
Property configuration();
Property data();
Property temporary();
Property cache();
Property log();
}
abstract class Mixin
implements FileConfigurationService
{
final Logger logger = LoggerFactory.getLogger( getClass().getName() );
@This
Data data;
@Uses
ServiceDescriptor descriptor;
@Structure
Application app;
@Override
public void resolveFileConfiguration()
{
OS os = detectOS();
data.os().set( os );
logger.info( "Operating system : " + os.name() );
// Get bundle with application name and configured directories
ResourceBundle bundle = ResourceBundle.getBundle( FileConfiguration.class.getName(), new Locale( os.name() ) );
Map arguments = getArguments( os );
data.configuration().set( new File( format( bundle.getString( "configuration" ), arguments ) ) );
data.data().set( new File( format( bundle.getString( "data" ), arguments ) ) );
data.temporary().set( new File( format( bundle.getString( "temporary" ), arguments ) ) );
data.cache().set( new File( format( bundle.getString( "cache" ), arguments ) ) );
data.log().set( new File( format( bundle.getString( "log" ), arguments ) ) );
applyOverride();
autoCreateDirectories();
}
private Map getArguments( OS os )
{
// Get user directory
String user = System.getenv( "USERPROFILE" ); // On Windows we use this instead of user.home
if ( user == null ) {
user = System.getProperty( "user.home" );
}
data.user().set( new File( user ) );
// Set application name. This is taken from the Zest application but can be overriden by a system property
String application = System.getProperty( "application", app.name() );
if ( !app.mode().equals( Application.Mode.production ) ) {
application += "-" + app.mode().name();
}
data.application().set( application );
// Temp dir
String temp = System.getProperty( "java.io.tmpdir" );
if ( temp.endsWith( "/" ) ) {
temp = temp.substring( 0, temp.length() - 1 );
}
// Arguments available to use in directory specifications
Map arguments = new HashMap();
arguments.put( "application", application );
arguments.put( "user", user );
arguments.put( "os", os.name() );
arguments.put( "temp", temp );
// Add environment variables
for ( Map.Entry envVariable : System.getenv().entrySet() ) {
arguments.put( "environment." + envVariable.getKey(), envVariable.getValue() );
}
// Add system properties
for ( Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy