Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * 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.lastaflute.di.core;
 
 import java.util.List;
 

Author(s):
jflute
 
 public class LastaDiProperties {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     public static final String LASTA_DI_PROPERTIES = "lasta_di.properties";
     public static final String SMART_DEPLOY_MODE_LOCATION_KEY = "smart.deploy.mode.location";
     public static final String SMART_PACKAGE1_KEY = "smart.package1";
     public static final String SMART_PACKAGE2_KEY = "smart.package2";
     public static final String SMART_PACKAGE3_KEY = "smart.package3";
     public static final String DIXML_SCRIPT_EXPRESSION_ENGINE_KEY = "dixml.script.expression.engine";
 
     private static final Logger logger = LoggerFactory.getLogger(LastaDiProperties.class);
     private static LastaDiProperties instance// lazy loaded
 
     public static LastaDiProperties getInstance() {
         if ( == null) {
             synchronized (LastaDiProperties.class) {
                 if ( == null) {
                      = new LastaDiProperties();
                 }
             }
         }
         return ;
     }
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final Properties props;
     protected String smartDeployMode// load loaded
     protected boolean smartDeployLocationDone;
     protected List<StringsmartPackageList// load loaded
     protected Class<?> diXmlScriptExpressionEngineType// load loaded
     protected boolean diXmlScriptExpressionEngineTypeDone;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     protected LastaDiProperties() {
         final String propName = ;
         final Properties read = loadProperties(propName);
         if (read != null) {
              = read;
         } else {
             .info("*Not found the {} in your classpath."propName);
              = new Properties();
         }
     }
 
     // ===================================================================================
     //                                                                            Property
     //                                                                            ========
     public String getProperty(String propertyKey) {
         return .getProperty(propertyKey);
     }
 
     // -----------------------------------------------------
     //                                          Smart Deploy
     //                                          ------------
     public String getSmartDeployMode() {
         if ( != null || ) {
             return ;
         }
         synchronized (this) {
             if ( != null || ) {
                return ;
            }
            final String locKey = ;
            final String location = getProperty(locKey);
            if (location != null && !location.isEmpty()) {
                final String delimiter = ":";
                final int delimiterIndex = location.indexOf(":");
                if (delimiterIndex < 0) {
                    String msg = "The location should have delimiter colon ':' in " +  + " but: " + location;
                    throw new IllegalStateException(msg);
                }
                final String propName = location.substring(0, delimiterIndex).trim();
                final String modeKey = location.substring(delimiterIndex + delimiter.length()).trim();
                .info("...Loading specified properties and get by the key: {}, {}"propNamemodeKey);
                final Properties read = loadProperties(propName);
                if (read == null) {
                    throwSmartDeployPropertiesFileNotFoundException(locationpropNamemodeKey);
                }
                final String realMode = read.getProperty(modeKey);
                if (realMode == null) {
                    throwSmartDeployPropertiesModeKeyNotFoundException(locationpropNamemodeKey);
                }
                 = realMode;
            } else {
                .info("*Not found the smart-deploy mode location: {} in {}"locKey);
            }
             = true;
            return ;
        }
    }
    protected void throwSmartDeployPropertiesFileNotFoundException(String locationString propNameString modeKey) {
        final LdiExceptionMessageBuilder br = new LdiExceptionMessageBuilder();
        br.addNotice("Not found the properties for smart deploy.");
        br.addItem("Advice");
        br.addElement("Make sure your properties or location setting.");
        br.addElement("  (x): at your " + );
        br.addElement("    " +  + " = no_exist.properties: lasta_di.smart.deploy.mode");
        br.addElement("");
        br.addElement("  (o): at your " +  + ", e.g. maihama project");
        br.addElement("    " +  + " = maihama_env.properties: lasta_di.smart.deploy.mode");
        br.addItem("Location");
        br.addElement(location);
        br.addItem("NotFound File");
        br.addElement(propName);
        br.addItem("Deploy Mode Key");
        br.addElement(modeKey);
        final String msg = br.buildExceptionMessage();
        throw new IllegalStateException(msg);
    }
    protected void throwSmartDeployPropertiesModeKeyNotFoundException(String locationString propNameString modeKey) {
        final LdiExceptionMessageBuilder br = new LdiExceptionMessageBuilder();
        br.addNotice("Not found the property in your smart deploy properties.");
        br.addItem("Advice");
        br.addElement("Make sure your property key setting.");
        br.addElement("  (x): at your " +  + ", e.g. maihama project");
        br.addElement("    " +  + " = maihama_env.properties: no_exist.smart.deploy.mode");
        br.addElement("");
        br.addElement("  (o): at your " +  + ", e.g. maihama project");
        br.addElement("    " +  + " = maihama_env.properties: lasta_di.smart.deploy.mode");
        br.addElement("");
        br.addElement("  (o): at your maihama_env.properties, e.g. maihama project");
        br.addElement("    lasta_di.smart.deploy.mode = hot");
        br.addItem("Location");
        br.addElement(location);
        br.addItem("Properties File");
        br.addElement(propName);
        br.addItem("NotFound Key");
        br.addElement(modeKey);
        final String msg = br.buildExceptionMessage();
        throw new IllegalStateException(msg);
    }
    // -----------------------------------------------------
    //                                         Smart Package
    //                                         -------------
    public List<StringgetSmartPackageList() {
        if ( != null) {
            return ;
        }
        synchronized (this) {
            if ( != null) {
                return ;
            }
            final List<StringpkgList = new ArrayList<String>(3);
            pkgList.add(getSmartPackage1());
            pkgList.add(getSmartPackage2());
            pkgList.add(getSmartPackage3());
             = pkgList.stream().filter(pkg -> pkg != null).collect(Collectors.toList());
            return Collections.unmodifiableList();
        }
    }
    public String getSmartPackage1() {
        return getProperty();
    }
    public String getSmartPackage2() {
        return getProperty();
    }
    public String getSmartPackage3() {
        return getProperty();
    }
    // -----------------------------------------------------
    //                                         Di XML Script
    //                                         -------------
    }
    public Class<?> getDiXmlScriptExpressionEngineType() { // null allowed
            synchronized (this) {
                if ( == null) {
                    final String engineName = getDiXmlScriptExpressionEngine();
                    if (engineName != null) {
                        // TODO jflute lastaflute: [E] fitting: DI :: expression engine property error handling
                         = LdiClassUtil.forName(engineName);
                    }
                     = true;
                }
            }
        }
        return ;
    }
    // ===================================================================================
    //                                                                        Small Helper
    //                                                                        ============
    protected Properties loadProperties(String fileName) {
        final Properties props = new Properties();
        final InputStream ins = LastaDiProperties.class.getClassLoader().getResourceAsStream(fileName);
        if (ins == null) {
            return null;
        }
        try {
            props.load(ins);
        } catch (RuntimeException | IOException e) {
            handleLoadingFailureException(fileNamee);
        } finally {
            try {
                ins.close();
            } catch (IOException ignored) {}
        }
        return props;
    }
    protected void handleLoadingFailureException(String fileNameException e) {
        final LdiExceptionMessageBuilder br = new LdiExceptionMessageBuilder();
        br.addNotice("Failed to load the properties.");
        br.addItem("Properties File");
        br.addElement(fileName);
        final String msg = br.buildExceptionMessage();
        throw new IllegalStateException(msge);
    }
New to GrepCode? Check out our FAQ X