KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > deployment > DescriptorFactory


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.deployment;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.jbi.config.spring.XBeanProcessor;
32 import org.apache.servicemix.jbi.util.FileUtil;
33 import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
34 import org.springframework.core.io.UrlResource;
35
36 /**
37  * @version $Revision: 359151 $
38  */

39 public class DescriptorFactory {
40
41     public static final String JavaDoc DESCRIPTOR_FILE = "META-INF/jbi.xml";
42
43     private static Log log = LogFactory.getLog(DescriptorFactory.class);
44
45     /**
46      * Build a jbi descriptor from a file archive
47      *
48      * @param descriptorFile
49      * path to the jbi descriptor, or to the root directory
50      * @return the Descriptor object
51      */

52     public static Descriptor buildDescriptor(File JavaDoc descriptorFile) {
53         if (descriptorFile.isDirectory()) {
54             descriptorFile = new File JavaDoc(descriptorFile, DESCRIPTOR_FILE);
55         }
56         if (descriptorFile.isFile()) {
57             try {
58                 return buildDescriptor(descriptorFile.toURL());
59             } catch (MalformedURLException JavaDoc e) {
60                 throw new RuntimeException JavaDoc("There is a bug here...", e);
61             }
62         }
63         return null;
64     }
65
66     /**
67      * Build a jbi descriptor from the specified URL
68      *
69      * @param url
70      * url to the jbi descriptor
71      * @return the Descriptor object
72      */

73     public static Descriptor buildDescriptor(URL JavaDoc url) {
74         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
75         try {
76             Thread.currentThread().setContextClassLoader(DescriptorFactory.class.getClassLoader());
77             ResourceXmlApplicationContext context = new ResourceXmlApplicationContext(
78                             new UrlResource(url),
79                             Arrays.asList(new Object JavaDoc[] { new XBeanProcessor() }));
80             Descriptor descriptor = (Descriptor) context.getBean("jbi");
81             checkDescriptor(descriptor);
82             return descriptor;
83         } finally {
84             Thread.currentThread().setContextClassLoader(cl);
85         }
86     }
87
88     /**
89      * Check validity of the JBI descriptor
90      *
91      * @param descriptor
92      * the descriptor to check
93      * @throws Exception
94      * if the descriptor is not valid
95      */

96     public static void checkDescriptor(Descriptor descriptor) {
97         List JavaDoc violations = new ArrayList JavaDoc();
98
99         if (descriptor.getVersion() != 1.0) {
100             violations.add("JBI descriptor version should be set to '1.0' but is " + descriptor.getVersion());
101         }
102
103         if (descriptor.getComponent() != null) {
104             checkComponent(violations, descriptor.getComponent());
105         } else if (descriptor.getServiceAssembly() != null) {
106             checkServiceAssembly(violations, descriptor.getServiceAssembly());
107         } else if (descriptor.getServices() != null) {
108             checkServiceUnit(violations, descriptor.getServices());
109         } else if (descriptor.getSharedLibrary() != null) {
110             checkSharedLibrary(violations, descriptor.getSharedLibrary());
111         } else {
112             violations.add("The jbi descriptor does not contain any informations");
113         }
114
115         if (violations.size() > 0) {
116             throw new RuntimeException JavaDoc("The JBI descriptor is not valid, please correct these violations "
117                             + violations.toString());
118         }
119     }
120
121     /**
122      * Checks that the component is valid
123      *
124      * @param violations
125      * A list of violations that the check can add to
126      *
127      * @param component
128      * The component descriptor that is being checked
129      */

130     private static void checkComponent(List JavaDoc violations, Component component) {
131         if (component.getIdentification() == null) {
132             violations.add("The component has not identification");
133         } else {
134             if (isBlank(component.getIdentification().getName())) {
135                 violations.add("The component name is not set");
136             }
137         }
138         if (component.getBootstrapClassName() == null) {
139             violations.add("The component has not defined a boot-strap class name");
140         }
141         if (component.getBootstrapClassPath() == null || component.getBootstrapClassPath().getPathElements() == null) {
142             violations.add("The component has not defined any boot-strap class path elements");
143         }
144     }
145
146     /**
147      * Checks that the service assembly is valid
148      *
149      * @param violations
150      * A list of violations that the check can add to
151      *
152      * @param serviceAssembly
153      * The service assembly descriptor that is being checked
154      */

155     private static void checkServiceAssembly(List JavaDoc violations, ServiceAssembly serviceAssembly) {
156         if (serviceAssembly.getIdentification() == null) {
157             violations.add("The service assembly has not identification");
158         } else {
159             if (isBlank(serviceAssembly.getIdentification().getName())) {
160                violations.add("The service assembly name is not set");
161             }
162         }
163     }
164
165     /**
166      * Checks that the service unit is valid
167      *
168      * @param violations
169      * A list of violations that the check can add to
170      *
171      * @param services
172      * The service unit descriptor that is being checked
173      */

174     private static void checkServiceUnit(List JavaDoc violations, Services services) {
175         // TODO Auto-generated method stub
176

177     }
178
179     /**
180      * Checks that the shared library is valid
181      *
182      * @param violations
183      * A list of violations that the check can add to
184      *
185      * @param sharedLibrary
186      * The shared library descriptor that is being checked
187      */

188     private static void checkSharedLibrary(List JavaDoc violations, SharedLibrary sharedLibrary) {
189         if (sharedLibrary.getIdentification() == null) {
190             violations.add("The shared library has not identification");
191         } else {
192             if (isBlank(sharedLibrary.getIdentification().getName())) {
193                violations.add("The shared library name is not set");
194             }
195         }
196     }
197
198     /**
199      * Retrieves the jbi descriptor as a string
200      *
201      * @param descriptorFile
202      * path to the jbi descriptor, or to the root directory
203      * @return the contents of the jbi descriptor
204      */

205     public static String JavaDoc getDescriptorAsText(File JavaDoc descriptorFile) {
206         if (descriptorFile.isDirectory()) {
207             descriptorFile = new File JavaDoc(descriptorFile, DESCRIPTOR_FILE);
208         }
209         if (descriptorFile.isFile()) {
210             try {
211                 ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
212                 InputStream JavaDoc is = new FileInputStream JavaDoc(descriptorFile);
213                 FileUtil.copyInputStream(is, os);
214                 return os.toString();
215             } catch (Exception JavaDoc e) {
216                 log.debug("Error reading jbi descritor: " + descriptorFile, e);
217             }
218         }
219         return null;
220     }
221
222     /**
223      * <p>Checks if a String is whitespace, empty ("") or null.</p>
224      *
225      * <pre>
226      * StringUtils.isBlank(null) = true
227      * StringUtils.isBlank("") = true
228      * StringUtils.isBlank(" ") = true
229      * StringUtils.isBlank("bob") = false
230      * StringUtils.isBlank(" bob ") = false
231      * </pre>
232      *
233      * @param str the String to check, may be null
234      * @return <code>true</code> if the String is null, empty or whitespace
235      *
236      * Copied from org.apache.commons.lang.StringUtils#isBlanck
237      */

238     private static boolean isBlank(String JavaDoc str) {
239         int strLen;
240         if (str == null || (strLen = str.length()) == 0) {
241             return true;
242         }
243         for (int i = 0; i < strLen; i++) {
244             if ((Character.isWhitespace(str.charAt(i)) == false)) {
245                 return false;
246             }
247         }
248         return true;
249     }
250
251 }
252
Popular Tags