1 17 package org.apache.servicemix.jbi.deployment; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.InputStream ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.List ; 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 39 public class DescriptorFactory { 40 41 public static final String DESCRIPTOR_FILE = "META-INF/jbi.xml"; 42 43 private static Log log = LogFactory.getLog(DescriptorFactory.class); 44 45 52 public static Descriptor buildDescriptor(File descriptorFile) { 53 if (descriptorFile.isDirectory()) { 54 descriptorFile = new File (descriptorFile, DESCRIPTOR_FILE); 55 } 56 if (descriptorFile.isFile()) { 57 try { 58 return buildDescriptor(descriptorFile.toURL()); 59 } catch (MalformedURLException e) { 60 throw new RuntimeException ("There is a bug here...", e); 61 } 62 } 63 return null; 64 } 65 66 73 public static Descriptor buildDescriptor(URL url) { 74 ClassLoader 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 [] { 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 96 public static void checkDescriptor(Descriptor descriptor) { 97 List violations = new ArrayList (); 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 ("The JBI descriptor is not valid, please correct these violations " 117 + violations.toString()); 118 } 119 } 120 121 130 private static void checkComponent(List 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 155 private static void checkServiceAssembly(List 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 174 private static void checkServiceUnit(List violations, Services services) { 175 177 } 178 179 188 private static void checkSharedLibrary(List 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 205 public static String getDescriptorAsText(File descriptorFile) { 206 if (descriptorFile.isDirectory()) { 207 descriptorFile = new File (descriptorFile, DESCRIPTOR_FILE); 208 } 209 if (descriptorFile.isFile()) { 210 try { 211 ByteArrayOutputStream os = new ByteArrayOutputStream (); 212 InputStream is = new FileInputStream (descriptorFile); 213 FileUtil.copyInputStream(is, os); 214 return os.toString(); 215 } catch (Exception e) { 216 log.debug("Error reading jbi descritor: " + descriptorFile, e); 217 } 218 } 219 return null; 220 } 221 222 238 private static boolean isBlank(String 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 |