Search in sources :

Example 1 with BundleConfigurationLoadingException

use of org.motechproject.osgi.web.exception.BundleConfigurationLoadingException in project motech by motech.

the class Log4JBundleLoader method loadBundle.

public void loadBundle(Bundle bundle) throws BundleConfigurationLoadingException, IOException {
    String symbolicName = bundle.getSymbolicName();
    LOGGER.debug("Looking for log4j config in {}", symbolicName);
    URL log4jUrl = bundle.getResource(log4JConf);
    if (log4jUrl != null) {
        LOGGER.debug("Log4j config found in {}, loading", symbolicName);
        InputStream log4jStream = null;
        try {
            URLConnection conn = log4jUrl.openConnection();
            log4jStream = conn.getInputStream();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
            Document log4jDoc = db.parse(log4jStream);
            if (loggers != null && checkLogXmlConfiguration(log4jDoc)) {
                PropertyConfigurator.configure(loggerProperties);
            } else {
                DOMConfigurator.configure(log4jDoc.getDocumentElement());
            }
            logService.reconfigure();
            LOGGER.debug("Added log4j configuration for [" + bundle.getLocation() + "]");
        } catch (ParserConfigurationException | SAXException e) {
            throw new BundleConfigurationLoadingException("Error while loading log4j configuration from " + bundle, e);
        } finally {
            IOUtils.closeQuietly(log4jStream);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) URLConnection(java.net.URLConnection) BundleConfigurationLoadingException(org.motechproject.osgi.web.exception.BundleConfigurationLoadingException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with BundleConfigurationLoadingException

use of org.motechproject.osgi.web.exception.BundleConfigurationLoadingException in project motech by motech.

the class BlueprintApplicationContextTracker method addingService.

@Override
public Object addingService(ServiceReference serviceReference) {
    ApplicationContext applicationContext = (ApplicationContext) super.addingService(serviceReference);
    Bundle bundle = serviceReference.getBundle();
    String symbolicName = nullSafeSymbolicName(bundle);
    LOGGER.info("Processing context for: {}", symbolicName);
    if (!isBlueprintEnabledBundle(bundle)) {
        LOGGER.debug("Bundle {} is not Blueprint Enabled", symbolicName);
        return applicationContext;
    }
    String contextServiceName = getServiceName(serviceReference);
    if (!symbolicName.equals(contextServiceName)) {
        LOGGER.warn("Bundle symbolic name [{}] does not match the service name of the context [{}]", symbolicName, contextServiceName);
        return applicationContext;
    }
    if (httpServiceTrackers.isBeingTracked(bundle)) {
        LOGGER.debug("Bundle {} is already tracked", symbolicName);
        return applicationContext;
    }
    LOGGER.debug("Registering trackers for {}", symbolicName);
    httpServiceTrackers.addTrackerFor(bundle);
    uiServiceTrackers.addTrackerFor(bundle, applicationContext);
    LOGGER.debug("Trackers registered for {}", symbolicName);
    // scan for logger configuration, we don't want to do this in tests
    if (!StringUtils.equalsIgnoreCase("true", System.getProperty(IGNORE_DB_VAR))) {
        LOGGER.debug("Scanning bundle {} for logger configuration", symbolicName);
        synchronized (this) {
            if (OSGI_WEB_UTIL.equals(symbolicName)) {
                logBundleLoader = applicationContext.getBean(Log4JBundleLoader.class);
            }
            try {
                BundleRegister bundleRegister = BundleRegister.getInstance();
                bundleRegister.addBundle(bundle);
                if (logBundleLoader != null) {
                    List<Bundle> bundleList = bundleRegister.getBundleList();
                    for (Bundle bundleElement : bundleList) {
                        logBundleLoader.loadBundle(bundleElement);
                    }
                    bundleRegister.getBundleList().clear();
                }
            } catch (BundleConfigurationLoadingException e) {
                LOGGER.error("Failed adding log4j configuration for [" + serviceReference.getBundle().getLocation() + "]\n" + e.getMessage());
            } catch (IOException e) {
                LOGGER.error(e.getMessage());
            }
        }
    }
    return applicationContext;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Log4JBundleLoader(org.motechproject.osgi.web.bundle.Log4JBundleLoader) Bundle(org.osgi.framework.Bundle) BundleRegister(org.motechproject.osgi.web.bundle.BundleRegister) IOException(java.io.IOException) BundleConfigurationLoadingException(org.motechproject.osgi.web.exception.BundleConfigurationLoadingException)

Aggregations

IOException (java.io.IOException)2 BundleConfigurationLoadingException (org.motechproject.osgi.web.exception.BundleConfigurationLoadingException)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 BundleRegister (org.motechproject.osgi.web.bundle.BundleRegister)1 Log4JBundleLoader (org.motechproject.osgi.web.bundle.Log4JBundleLoader)1 Bundle (org.osgi.framework.Bundle)1 ApplicationContext (org.springframework.context.ApplicationContext)1 Document (org.w3c.dom.Document)1 EntityResolver (org.xml.sax.EntityResolver)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1