/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI 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.openengsb.infrastructure.jms.internal;

import java.io.File;
import java.util.Dictionary;

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Activator implements BundleActivator {
    private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);

    private BrokerService brokerService;

    Thread startThread;

    @Override
    public void start(final BundleContext context) throws Exception {
        brokerService = new BrokerService();
        brokerService.setBrokerName("openengsb");

        String karafData = System.getProperty("karaf.data");
        brokerService.setDataDirectory(new File(karafData, "/activemq/openengsb").getAbsolutePath());
        KahaDBPersistenceAdapter persistenceAdapter = new KahaDBPersistenceAdapter();
        persistenceAdapter.setDirectory(new File(karafData, "/activemq/openengsb/kahadb"));
        brokerService.setPersistenceAdapter(persistenceAdapter);

        brokerService.getManagementContext().setCreateConnector(true);

        brokerService.getSystemUsage().getMemoryUsage().setLimit(20 * 1024 * 1024);
        brokerService.getSystemUsage().getStoreUsage().setLimit(1024 * 1024 * 1024);
        brokerService.getSystemUsage().getTempUsage().setLimit(100 * 1024 * 1024);

        final ServiceTracker serviceTracker = new ServiceTracker(context, ConfigurationAdmin.class.getName(), null);
        serviceTracker.open();
        startThread = new Thread() {
            public void run() {
                try {
                    ConfigurationAdmin configadmin = (ConfigurationAdmin) serviceTracker.waitForService(60000);
                    Configuration configuration = configadmin.getConfiguration("org.openengsb.infrastructure.jms");
                    @SuppressWarnings("unchecked")
                    Dictionary<String, Object> props = configuration.getProperties();
                    brokerService.addConnector("tcp://0.0.0.0:" + props.get("openwire"));
                    brokerService.addConnector("stomp://0.0.0.0:" + props.get("stomp"));
                    brokerService.start();
                } catch (Exception e) {
                    LOGGER.error("could not initialize brokerService", e);
                }
            };
        };
        startThread.start();
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        brokerService.stop();
    }

}

@@ -1124,16 +1124,208 @@
 ontext;%0A
+import org.osgi.framework.Constants;%0Aimport org.osgi.framework.FrameworkUtil;%0Aimport org.osgi.framework.InvalidSyntaxException;%0Aimport org.osgi.service.blueprint.container.BlueprintContainer;%0A
 import o
@@ -1354,24 +1354,24 @@
 figuration;%0A
-
 import org.o
@@ -2627,156 +2627,8 @@
 );%0A%0A
-        final ServiceTracker serviceTracker = new ServiceTracker(context, ConfigurationAdmin.class.getName(), null);%0A        serviceTracker.open();%0A
     
@@ -2656,24 +2656,46 @@
  Thread() %7B%0A
+            @Override%0A
             
@@ -2736,16 +2736,261 @@
   try %7B%0A
+                    ServiceTracker serviceTracker =%0A                        new ServiceTracker(context, ConfigurationAdmin.class.getName(), null);%0A                    waitForBlueprintToFinish(context);%0A                    serviceTracker.open();%0A
         
@@ -3683,16 +3683,16 @@
 e%22, e);%0A
-
         
@@ -3701,16 +3701,1073 @@
       %7D%0A
+            %7D%0A%0A            // FIXME ARIES-875%0A            //%0A            // This is a workaround for ARIES-875. It must wait for the blueprint-extender to finish the initialization,%0A            // so that it does not interfere with the ServiceReference to the ConfigurationAdmin%0A            private void waitForBlueprintToFinish(final BundleContext context) throws InvalidSyntaxException,%0A                InterruptedException %7B%0A                ServiceTracker blueprintTracker = new ServiceTracker(context, FrameworkUtil.createFilter(%0A                    String.format(%22(&(%25s=%25s)(%25s=%25s)(%25s=%25s))%22,%0A                        Constants.OBJECTCLASS, BlueprintContainer.class.getName(),%0A                        %22osgi.blueprint.container.symbolicname%22, context.getBundle().getSymbolicName(),%0A                        %22osgi.blueprint.container.version%22, context.getBundle().getVersion().toString()))%0A                    , null);%0A                blueprintTracker.open();%0A                blueprintTracker.waitForService(30000);%0A                blueprintTracker.close();%0A
         

make sure blueprint-config is finished before creating Brokers in infrastructure-jms