Search in sources :

Example 1 with Logger

use of org.apache.felix.framework.Logger in project sling by apache.

the class SlingServletDelegate method init.

/**
     * Initializes this servlet by loading the framework configuration
     * properties, starting the OSGi framework (Apache Felix) and exposing the
     * system bundle context and the <code>Felix</code> instance as servlet
     * context attributes.
     *
     * @throws ServletException if the framework cannot be initialized.
     */
@Override
public final void init() throws ServletException {
    // temporary holders control final setup and ensure proper
    // disposal in case of setup errors
    Sling tmpSling = null;
    Servlet tmpDelegatee = null;
    try {
        log("Starting Apache Sling in " + slingHome);
        // read the default parameters
        Map<String, String> props = loadConfigProperties(slingHome);
        Logger logger = new ServletContextLogger(getServletContext());
        LaunchpadContentProvider rp = new ServletContextResourceProvider(getServletContext());
        tmpSling = SlingBridge.getSlingBridge(notifiable, logger, rp, props, getServletContext());
        // set up the OSGi HttpService proxy servlet
        tmpDelegatee = new ProxyServlet();
        tmpDelegatee.init(getServletConfig());
        // them destroyed in the finally clause.
        if (servletDestroyed) {
            log("SlingServletDelegate destroyed while starting Apache Sling, shutting Apache Sling down");
        } else {
            // set the fields now
            sling = tmpSling;
            delegatee = tmpDelegatee;
            // reset temporary holders to prevent destroyal
            tmpSling = null;
            tmpDelegatee = null;
            log("Apache Sling successfully started in " + slingHome);
        }
    } catch (BundleException be) {
        throw new ServletException("Failed to start Apache Sling in " + slingHome, be);
    } catch (ServletException se) {
        throw new ServletException("Failed to start bridge servlet for Apache Sling", se);
    } catch (Throwable t) {
        throw new ServletException("Uncaught Failure starting Apache Sling", t);
    } finally {
        // clean up temporary fields
        if (tmpDelegatee != null) {
            tmpDelegatee.destroy();
        }
        if (tmpSling != null) {
            tmpSling.destroy();
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) LaunchpadContentProvider(org.apache.sling.launchpad.api.LaunchpadContentProvider) ProxyServlet(org.apache.felix.http.proxy.ProxyServlet) ProxyServlet(org.apache.felix.http.proxy.ProxyServlet) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet) BundleException(org.osgi.framework.BundleException) Logger(org.apache.felix.framework.Logger) Sling(org.apache.sling.launchpad.base.impl.Sling)

Example 2 with Logger

use of org.apache.felix.framework.Logger in project sling by apache.

the class MainDelegate method start.

@Override
public boolean start() {
    Map<String, String> props = new HashMap<String, String>();
    // parse the command line (exit in case of failure)
    if (commandLine == null) {
        setCommandLine(new HashMap<String, String>());
    }
    // if sling.home was set on the command line, set it in the properties
    if (slingHome != null) {
        props.put(SharedConstants.SLING_HOME, slingHome);
    } else if (commandLine.containsKey(SharedConstants.SLING_HOME)) {
        props.put(SharedConstants.SLING_HOME, commandLine.get(SharedConstants.SLING_HOME));
    }
    // ensure sling.launchpad is set
    if (!commandLine.containsKey(SharedConstants.SLING_LAUNCHPAD)) {
        commandLine.put(SharedConstants.SLING_LAUNCHPAD, slingHome);
    }
    // check sling.properties in the command line
    final String slingPropertiesProp = commandLine.remove(SharedConstants.SLING_PROPERTIES);
    if (slingPropertiesProp != null) {
        props.put(SharedConstants.SLING_PROPERTIES, slingPropertiesProp);
    }
    // set up and configure Felix Logger
    int logLevel;
    if (!commandLine.containsKey(PROP_LOG_LEVEL)) {
        logLevel = DEFAULT_LOG_LEVEL;
    } else {
        logLevel = toLogLevelInt(commandLine.get(PROP_LOG_LEVEL), DEFAULT_LOG_LEVEL);
        commandLine.put(LOG_LEVEL_PROP, String.valueOf(logLevel));
    }
    final Logger logger = new SlingLogger();
    // default log level: prevent tons of needless WARN from the framework
    logger.setLogLevel(Logger.LOG_ERROR);
    if (System.getProperty(PROP_BOOT_LOG_LEVEL) != null) {
        try {
            logger.setLogLevel(Integer.parseInt(System.getProperty(PROP_BOOT_LOG_LEVEL)));
        } catch (final NumberFormatException ex) {
        // just ignore
        }
    }
    try {
        LaunchpadContentProvider resProvider = new ClassLoaderResourceProvider(getClass().getClassLoader());
        // creating the instance launches the framework and we are done here
        // ..
        sling = new Sling(notifiable, logger, resProvider, props) {

            // overwrite the loadPropertiesOverride method to inject the
            // command line arguments unconditionally. These will not be
            // persisted in any properties file, though
            @Override
            protected void loadPropertiesOverride(Map<String, String> properties) {
                if (commandLine != null) {
                    properties.putAll(commandLine);
                }
                // Display port number on console, in case HttpService doesn't. This is logged as late as
                // possible in order to pick up defaults from the Sling property files, although system
                // property substitutions will be missed.
                info("HTTP server port: " + properties.get(PROP_PORT), null);
            }
        };
        // we successfully started it
        return true;
    } catch (BundleException be) {
        error("Failed to Start OSGi framework", be);
    }
    // we failed to start
    return false;
}
Also used : HashMap(java.util.HashMap) Logger(org.apache.felix.framework.Logger) Sling(org.apache.sling.launchpad.base.impl.Sling) LaunchpadContentProvider(org.apache.sling.launchpad.api.LaunchpadContentProvider) ClassLoaderResourceProvider(org.apache.sling.launchpad.base.impl.ClassLoaderResourceProvider) BundleException(org.osgi.framework.BundleException)

Example 3 with Logger

use of org.apache.felix.framework.Logger in project sling by apache.

the class SlingFelix method getPropsAndDefaultProps.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static Map getPropsAndDefaultProps(final Map props) {
    final Logger logger = (Logger) props.get(FelixConstants.LOG_LOGGER_PROP);
    if (logger != null) {
        final Properties fullProps = new Properties();
        final Properties defaultProps = Util.loadDefaultProperties(logger);
        fullProps.putAll(defaultProps);
        fullProps.putAll(props);
        // replace variables
        for (final Object name : defaultProps.keySet()) {
            if (!props.containsKey(name)) {
                final String value = (String) fullProps.get(name);
                final String substValue = Util.substVars(value, name.toString(), null, fullProps);
                fullProps.put(name, substValue);
            }
        }
        return fullProps;
    }
    return props;
}
Also used : Logger(org.apache.felix.framework.Logger) Properties(java.util.Properties)

Example 4 with Logger

use of org.apache.felix.framework.Logger in project sling by apache.

the class StartupManagerTimestampTest method setup.

@Before
public void setup() throws IOException {
    final File tmpFile = File.createTempFile(getClass().getSimpleName(), "tmp");
    final String tmpDirName = tmpFile.getParentFile().getAbsolutePath();
    try {
        final Map<String, String> properties = new HashMap<String, String>();
        properties.put(SharedConstants.SLING_HOME, tmpDirName);
        properties.put(Constants.FRAMEWORK_STORAGE, tmpDirName);
        properties.put(Constants.FRAMEWORK_BEGINNING_STARTLEVEL, "42");
        final Logger logger = new Logger();
        startupManager = new StartupManager(properties, logger);
    } finally {
        tmpFile.delete();
    }
}
Also used : HashMap(java.util.HashMap) Logger(org.apache.felix.framework.Logger) File(java.io.File) Before(org.junit.Before)

Example 5 with Logger

use of org.apache.felix.framework.Logger in project sling by apache.

the class AbstractLaunchpadStartingMojo method executeWithArtifacts.

/**
     * {@inheritDoc}
     */
@Override
protected void executeWithArtifacts() throws MojoExecutionException {
    try {
        final Map<String, String> props = new HashMap<String, String>();
        props.put(SharedConstants.SLING_HOME, slingHome);
        // ensure launchpad is set
        props.put(SharedConstants.SLING_LAUNCHPAD, slingHome);
        if (forceBundleLoad) {
            props.put(SharedConstants.FORCE_PACKAGE_BUNDLE_LOADING, "true");
        }
        // set up and configure Felix Logger
        int logLevelNum;
        if (logLevel == null) {
            logLevelNum = DEFAULT_LOG_LEVEL;
        } else {
            logLevelNum = toLogLevelInt(logLevel, DEFAULT_LOG_LEVEL);
        }
        props.put(LOG_LEVEL_PROP, String.valueOf(logLevelNum));
        // Display port number on console, in case HttpService doesn't
        getLog().info("HTTP server port: " + httpPort);
        props.put(PROP_PORT, String.valueOf(httpPort));
        // prevent tons of needless WARN from the framework
        Logger logger = new Logger();
        logger.setLogLevel(Logger.LOG_ERROR);
        if (propertiesFile.exists()) {
            File tmp = null;
            try {
                tmp = File.createTempFile("sling", "props");
                mavenFileFilter.copyFile(propertiesFile, tmp, true, project, Collections.EMPTY_LIST, true, System.getProperty("file.encoding"), mavenSession);
                Properties loadedProps = PropertyUtils.loadPropertyFile(tmp, null);
                for (Object key : loadedProps.keySet()) {
                    props.put((String) key, (String) loadedProps.get(key));
                }
            } catch (IOException e) {
                throw new MojoExecutionException("Unable to create filtered properties file", e);
            } catch (MavenFilteringException e) {
                throw new MojoExecutionException("Unable to create filtered properties file", e);
            } finally {
                if (tmp != null) {
                    tmp.delete();
                }
            }
        }
        sling = startSling(resourceProvider, props, logger);
    } catch (BundleException be) {
        getLog().error("Failed to Start OSGi framework", be);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) Logger(org.apache.felix.framework.Logger) Properties(java.util.Properties) File(java.io.File)

Aggregations

Logger (org.apache.felix.framework.Logger)5 HashMap (java.util.HashMap)3 BundleException (org.osgi.framework.BundleException)3 File (java.io.File)2 Properties (java.util.Properties)2 LaunchpadContentProvider (org.apache.sling.launchpad.api.LaunchpadContentProvider)2 Sling (org.apache.sling.launchpad.base.impl.Sling)2 IOException (java.io.IOException)1 GenericServlet (javax.servlet.GenericServlet)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 ProxyServlet (org.apache.felix.http.proxy.ProxyServlet)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenFilteringException (org.apache.maven.shared.filtering.MavenFilteringException)1 ClassLoaderResourceProvider (org.apache.sling.launchpad.base.impl.ClassLoaderResourceProvider)1 Before (org.junit.Before)1