001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.osgi;
022
023import java.util.Dictionary;
024import java.util.Hashtable;
025
026import org.granite.config.api.Configuration;
027import org.granite.config.api.internal.ConfigurationImpl;
028import org.granite.logging.Logger;
029import org.granite.osgi.adaptor.AMFServiceAdaptor;
030import org.granite.osgi.constants.OSGIConstants;
031import org.granite.osgi.metadata.ManifestMetadataParser;
032import org.osgi.framework.BundleActivator;
033import org.osgi.framework.BundleContext;
034import org.osgi.framework.ServiceReference;
035import org.osgi.framework.ServiceRegistration;
036import org.osgi.service.http.HttpService;
037import org.osgi.util.tracker.ServiceTracker;
038
039/**
040 * @author <a href="mailto:gembin@gmail.com">gembin@gmail.com</a>
041 * @since 1.1.0
042 */
043public class Activator implements BundleActivator {
044
045        private static final Logger log=Logger.getLogger(Activator.class);
046        private static final String DEFAULT_CONTEXT_PATH="/WebContent";
047        static ServiceTracker configurationTracker;
048
049        String contextPath;
050        ServiceRegistration configRegistration;
051        ServiceTracker httpServiceTracker;
052        ManifestMetadataParser metaParser;
053
054        /*
055         * (non-Javadoc)
056         * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
057         */
058        public void start(BundleContext context) throws Exception {
059                contextPath=(String) context.getBundle().getHeaders().get(OSGIConstants.GDS_CONTEXT);
060                contextPath=(contextPath==null?DEFAULT_CONTEXT_PATH:contextPath);
061                if(!contextPath.startsWith("/")){
062                        contextPath="/"+contextPath;
063                }
064                Configuration cfg=new ConfigurationImpl();
065                //provide a service for other bundle to override the config files
066                configRegistration=context.registerService(Configuration.class.getName(),cfg, null);
067                configurationTracker=new ServiceTracker(context,Configuration.class.getName(),null);
068                configurationTracker.open();
069                //set default config files
070                setupDefaultConfigurations(cfg);
071                
072                //track the AMFServiceAdaptor
073                httpServiceTracker=new HttpServiceTracker(context);
074                httpServiceTracker.open();
075                //begin to parse Metadata for all bundle in the OSGi Container
076                metaParser=new ManifestMetadataParser(context);
077                metaParser.start();
078        }
079        /**
080         * 
081         * @param cfg
082         */
083        private void setupDefaultConfigurations(Configuration cfg){
084                cfg.setFlexServicesConfig(OSGIConstants.DEFAULT_FLEX_CONFIG);
085                cfg.setGraniteConfig(OSGIConstants.DEFAULT_GRANITEDS_CONFIG);
086        }
087        /**
088         * @return Configuration
089         */
090        public static Configuration getConfigurationService(){
091                return (Configuration) configurationTracker.getService();
092        }
093        /**
094         * Register AMFServiceAdaptor
095         * HttpService Tracker
096         */
097        private class HttpServiceTracker extends ServiceTracker {
098                String amfServicServleteAlias=contextPath+"/graniteamf/amf";
099                public HttpServiceTracker(BundleContext context) { 
100                        super(context, HttpService.class.getName(), null);
101                }
102                /*
103                 * (non-Javadoc)
104                 * @see org.osgi.util.tracker.ServiceTracker#addingService(org.osgi.framework.ServiceReference)
105                 */
106                @Override
107                public Object addingService(ServiceReference reference) {
108                        final HttpService httpService = (HttpService) context.getService(reference);
109                        try {
110                                Dictionary<String, Object> initparams = new Hashtable<String, Object>();
111                            initparams.put("servlet-name", "AMFServiceServlet");
112                            httpService.registerServlet(amfServicServleteAlias,new AMFServiceAdaptor(context), initparams, httpService.createDefaultHttpContext()); 
113                        } catch (Exception e) {
114                                log.error(e, "Could not add service");
115                        }
116                        return httpService;
117                }
118                /*
119                 * (non-Javadoc)
120                 * @see org.osgi.util.tracker.ServiceTracker#removedService(org.osgi.framework.ServiceReference, java.lang.Object)
121                 */
122                @Override
123                public void removedService(ServiceReference reference, Object service) {
124                        final HttpService httpService = (HttpService) service;
125                        httpService.unregister(amfServicServleteAlias);
126                        super.removedService(reference, service);
127                }
128        } 
129        /*
130         * (non-Javadoc)
131         * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
132         */
133        public void stop(BundleContext context) throws Exception {
134                if(configRegistration!=null){
135                        configRegistration.unregister();
136                        configRegistration=null;
137                }
138                if(configurationTracker!=null){
139                        configurationTracker.close();
140                        configurationTracker=null;
141                }
142                if(httpServiceTracker!=null){
143                        httpServiceTracker.close();
144                        httpServiceTracker=null;
145                }
146                if(metaParser!=null){
147                        metaParser.stop();
148                        metaParser=null;
149                }
150        }
151}