KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > InstallationContextImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.framework;
18
19 import org.apache.servicemix.jbi.deployment.Component;
20 import org.apache.servicemix.jbi.deployment.InstallationDescriptorExtension;
21 import org.apache.servicemix.jbi.deployment.SharedLibraryList;
22 import org.w3c.dom.DocumentFragment JavaDoc;
23
24 import javax.jbi.component.Bootstrap;
25 import javax.jbi.component.ComponentContext;
26 import javax.jbi.component.InstallationContext;
27
28 import java.io.File JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * This context contains information necessary for a JBI component to perform its installation/uninstallation
37  * processing. This is provided to the init() method of the component {@link Bootstrap}interface.
38  *
39  * @version $Revision: 426415 $
40  */

41 public class InstallationContextImpl implements InstallationContext {
42     
43     private Component descriptor;
44     private File JavaDoc installRoot;
45     private List classPathElements = Collections.EMPTY_LIST;
46     private ComponentContext context;
47     private boolean install = true;
48     
49     public InstallationContextImpl(Component descriptor) {
50         this.descriptor = descriptor;
51         if (descriptor.getComponentClassPath() != null &&
52             descriptor.getComponentClassPath().getPathElements() != null &&
53             descriptor.getComponentClassPath().getPathElements().length > 0) {
54             String JavaDoc[] elems = descriptor.getComponentClassPath().getPathElements();
55             for (int i = 0; i < elems.length; i++) {
56                 if (File.separatorChar == '\\') {
57                     elems[i] = elems[i].replace('/', '\\');
58                 } else {
59                     elems[i] = elems[i].replace('\\', '/');
60                 }
61             }
62             setClassPathElements(Arrays.asList(elems));
63         }
64     }
65
66     /**
67      * @return the descriptor
68      */

69     public Component getDescriptor() {
70         return descriptor;
71     }
72
73     /**
74      * @return the sharedLibraries
75      */

76     public String JavaDoc[] getSharedLibraries() {
77         return getSharedLibraries(descriptor.getSharedLibraries());
78     }
79
80     /**
81      * Get the name of the class that implements the {@link Component}interface for this component. This must be the
82      * component class name given in the component's installation descriptor.
83      *
84      * @return the {@link Component}implementation class name, which must be non-null and non-empty.
85      */

86     public String JavaDoc getComponentClassName() {
87         return descriptor.getComponentClassName();
88     }
89
90     /**
91      * Get a list of elements that comprise the class path for this component. Each element represents either a
92      * directory (containing class files) or a library file. All elements are reachable from the install root. These
93      * elements represent class path items that the component's execution-time component class loader uses, in search
94      * order. All path elements must use the file separator character appropriate to the system (i.e.,
95      * <code>File.separator</code>).
96      *
97      * @return a list of String objects, each of which contains a class path elements. The list must contain at least
98      * one class path element.
99      */

100     public List getClassPathElements() {
101         return classPathElements;
102     }
103
104     /**
105      * Get the unique name assigned to this component. This name must be assigned from the component's installation
106      * descriptor identification section.
107      *
108      * @return the unique component name, which must be non-null and non-empty.
109      */

110     public String JavaDoc getComponentName() {
111         return descriptor.getIdentification().getName();
112     }
113
114     /**
115      * Get the JBI context for this component. The following methods are valid to use on the context:
116      * <ul>
117      * <li>{@link ComponentContext#getMBeanNames()}</li>
118      * <li>{@link ComponentContext#getMBeanServer()}</li>
119      * <li>{@link ComponentContext#getNamingContext()}</li>
120      * <li>{@link ComponentContext#getTransactionManager()}</li>
121      * </ul>
122      * All other methods on the returned context must throw a <code>IllegalStateException</code> exception if invoked.
123      *
124      * @return the JBI context for this component, which must be non-null.
125      */

126     public ComponentContext getContext() {
127         return context;
128     }
129
130     /**
131      * Get the installation root directory full path name for this component. This path name must be formatted for the
132      * platform the JBI environment is running on.
133      *
134      * @return the installation root directory name, which must be non-null and non-empty.
135      */

136     public String JavaDoc getInstallRoot() {
137         return installRoot != null ? installRoot.getAbsolutePath() : ".";
138     }
139     
140     /**
141      *
142      * @return Return the install root
143      */

144     public File JavaDoc getInstallRootAsDir(){
145         return installRoot;
146     }
147
148     /**
149      * Return a DOM document fragment representing the installation descriptor (jbi.xml) extension data for the
150      * component, if any.
151      * <p>
152      * The Installation Descriptor Extension data are located at the end of the &lt;component&gt; element of the
153      * installation descriptor.
154      *
155      * @return a DOM document fragment containing the installation descriptor (jbi.xml) extension data, or
156      * <code>null</code> if none is present in the descriptor.
157      */

158     public DocumentFragment JavaDoc getInstallationDescriptorExtension() {
159         InstallationDescriptorExtension desc = descriptor.getDescriptorExtension();
160         return desc != null ? desc.getDescriptorExtension() : null;
161     }
162
163     /**
164      * Returns <code>true</code> if this context was created in order to install a component into the JBI environment.
165      * Returns <code>false</code> if this context was created to uninstall a previously installed component.
166      * <p>
167      * This method is provided to allow {@link Bootstrap}implementations to tailor their behaviour according to use
168      * case. For example, the {@link Bootstrap#init(InstallationContext)}method implementation may create different
169      * types of extension MBeans, depending on the use case specified by this method.
170      *
171      * @return <code>true</code> if this context was created in order to install a component into the JBI environment;
172      * otherwise the context was created to uninstall an existing component.
173      */

174     public boolean isInstall() {
175         return install;
176     }
177
178     /**
179      * Set the list of elements that comprise the class path for this component. Each element represents either a
180      * directory (containing class files) or a library file. Elements are reached from the install root. These elements
181      * represent class path items that the component's execution-time component class loader uses, in search order. All
182      * file paths are relative to the install root of the component.
183      * <p>
184      * This method allows the component's bootstrap to alter the execution-time class path specified by the component's
185      * installation descriptor. The component configuration determined during installation can affect the class path
186      * needed by the component at execution-time. All path elements must use the file separator character appropriate to
187      * the system (i.e., <code>File.separator</code>.
188      *
189      * @param classPathElements a list of String objects, each of which contains a class path elements; the list must be
190      * non-null and contain at least one class path element.
191      * @exception IllegalArgumentException if the class path elements is null, empty, or if an individual element is
192      * ill-formed.
193      */

194     public void setClassPathElements(java.util.List JavaDoc classPathElements) {
195         if (classPathElements == null) {
196             throw new IllegalArgumentException JavaDoc("classPathElements is null");
197         }
198         if (classPathElements.isEmpty()) {
199             throw new IllegalArgumentException JavaDoc("classPathElements is empty");
200         }
201         for (Iterator JavaDoc iter = classPathElements.iterator(); iter.hasNext();) {
202             Object JavaDoc obj = iter.next();
203             if (obj instanceof String JavaDoc == false) {
204                 throw new IllegalArgumentException JavaDoc("classPathElements must contain element of type String");
205             }
206             String JavaDoc element = (String JavaDoc) obj;
207             String JavaDoc sep = File.separator.equals("\\") ? "/" : "\\";
208             int offset = element.indexOf(sep);
209             if ( offset > -1 ) {
210                 throw new IllegalArgumentException JavaDoc("classPathElements contains an invalid file separator '" + sep + "'");
211             }
212             File JavaDoc f = new File JavaDoc((String JavaDoc) element);
213             if (f.isAbsolute()) {
214                 throw new IllegalArgumentException JavaDoc("classPathElements should not contain absolute paths");
215             }
216         }
217         this.classPathElements = new ArrayList JavaDoc(classPathElements);
218     }
219     
220     
221     /**
222      * @param context The context to set.
223      */

224     public void setContext(ComponentContext context) {
225         this.context = context;
226     }
227     /**
228      * @param install The install to set.
229      */

230     public void setInstall(boolean install) {
231         this.install = install;
232     }
233     /**
234      * @param installRoot The installRoot to set.
235      */

236     public void setInstallRoot(File JavaDoc installRoot) {
237         this.installRoot = installRoot;
238     }
239     /**
240      * @return Returns the binding.
241      */

242     public boolean isBinding() {
243         return descriptor.isBindingComponent();
244     }
245     /**
246      * @return Returns the engine.
247      */

248     public boolean isEngine() {
249         return descriptor.isServiceEngine();
250     }
251     /**
252      * @return Returns the componentDescription.
253      */

254     public String JavaDoc getComponentDescription() {
255         return descriptor.getIdentification().getDescription();
256     }
257
258     private static String JavaDoc[] getSharedLibraries(SharedLibraryList[] sharedLibraries) {
259         if (sharedLibraries == null || sharedLibraries.length == 0) {
260             return null;
261         }
262         String JavaDoc[] names = new String JavaDoc[sharedLibraries.length];
263         for (int i = 0; i < names.length; i++) {
264             names[i] = sharedLibraries[i].getName();
265         }
266         return names;
267     }
268
269 }
270
Popular Tags