001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.geronimo;
018    
019    import java.net.URI;
020    import java.net.URL;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.gbean.GBeanLifecycle;
027    
028    public class Component implements GBeanLifecycle {
029    
030        private static final Log log = LogFactory.getLog(Component.class);
031    
032        private String name;
033    
034        private String description;
035    
036        private String type;
037    
038        private String className;
039    
040        private Container container;
041    
042        private URI rootDir;
043    
044        private URI installDir;
045    
046        private URI workDir;
047    
048        private javax.jbi.component.Component component;
049    
050        private ClassLoader classLoader;
051    
052        public Component(String name, String description, String type, String className, Container container,
053                        URL configurationBaseUrl, ClassLoader classLoader) throws Exception {
054            this.name = name;
055            this.description = description;
056            this.type = type;
057            this.className = className;
058            this.container = container;
059            // TODO is there a simpler way to do this?
060            if (configurationBaseUrl.getProtocol().equalsIgnoreCase("file")) {
061                this.rootDir = new URI("file", configurationBaseUrl.getPath(), null);
062            } else {
063                this.rootDir = URI.create(configurationBaseUrl.toString());
064            }
065            this.installDir = rootDir.resolve("install/");
066            this.workDir = rootDir.resolve("workspace/");
067            this.classLoader = classLoader;
068            log.info("Created JBI component: " + name);
069        }
070    
071        public void doStart() throws Exception {
072            log.info("doStart called for JBI component: " + name);
073            try {
074                component = (javax.jbi.component.Component) classLoader.loadClass(className).newInstance();
075                container.register(this);
076            } catch (ClassNotFoundException e) {
077                log.error(classLoader);
078            }
079        }
080    
081        public void doStop() throws Exception {
082            log.info("doStop called for JBI component: " + name);
083            container.unregister(this);
084            component = null;
085        }
086    
087        public void doFail() {
088            log.info("doFail called for JBI component: " + name);
089            component = null;
090        }
091    
092        public String getType() {
093            return type;
094        }
095    
096        public String getName() {
097            return name;
098        }
099    
100        public String getDescription() {
101            return description;
102        }
103    
104        public URI getInstallDir() {
105            return installDir;
106        }
107    
108        public URI getWorkDir() {
109            return workDir;
110        }
111    
112        public URI getRootDir() {
113            return rootDir;
114        }
115    
116        public javax.jbi.component.Component getComponent() {
117            return component;
118        }
119    
120        public static final GBeanInfo GBEAN_INFO;
121    
122        static {
123            GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("JBIComponent", Component.class, "JBIComponent");
124            infoFactory.addAttribute("name", String.class, true);
125            infoFactory.addAttribute("description", String.class, true);
126            infoFactory.addAttribute("type", String.class, true);
127            infoFactory.addAttribute("className", String.class, true);
128            infoFactory.addReference("container", Container.class);
129            infoFactory.addAttribute("configurationBaseUrl", URL.class, true);
130            infoFactory.addAttribute("classLoader", ClassLoader.class, false);
131            infoFactory.setConstructor(new String[] { "name", "description", "type", "className", "container",
132                            "configurationBaseUrl", "classLoader" });
133            GBEAN_INFO = infoFactory.getBeanInfo();
134        }
135    
136        public static GBeanInfo getGBeanInfo() {
137            return GBEAN_INFO;
138        }
139    
140    }