1 package org.apache.beehive.wsm.model.jsr181; 2 3 20 21 import java.io.File ; 22 import java.io.FilenameFilter ; 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 29 import com.sun.mirror.apt.AnnotationProcessorEnvironment; 30 import com.sun.mirror.apt.Filer; 31 32 import java.net.URL ; 33 34 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata; 35 36 39 public class Jsr181ObjectModelStore { 40 41 private final static String EXTENSION = ".ser"; 42 private final static String LOCATOR = ".webservices"; 43 44 private static boolean isMarked = false; 45 46 private AnnotationProcessorEnvironment env; 47 48 53 public Jsr181ObjectModelStore(AnnotationProcessorEnvironment env) { 54 if (null == env) { 55 throw new IllegalArgumentException ("illegal apt environment: <null>"); 56 } 57 this.env = env; 58 } 59 60 65 public static File getLocation(String className) { 66 return new File (className + EXTENSION); 67 } 68 69 74 public static BeehiveWsTypeMetadata load(Class clazz) 75 throws IOException , ClassNotFoundException 76 { 77 String resourceName = getLocation(clazz.getName()).toString(); 78 URL url = clazz.getClassLoader().getResource(resourceName); 79 return load(url.openStream()); 80 } 81 82 87 public static BeehiveWsTypeMetadata load(InputStream is) 88 throws IOException , ClassNotFoundException 89 { 90 Jsr181TypeMetadataImpl objectModel = null; 91 ObjectInputStream ois = null; 92 93 try { 94 if (null == is) { 95 throw new IOException ("cannot load object model without input stream"); 96 } 97 ois = new ObjectInputStream (is); 98 objectModel = (Jsr181TypeMetadataImpl) ois.readObject(); 99 } 100 finally { 101 if (null != ois) { 102 ois.close(); 103 } 104 } 105 106 return objectModel; 107 } 108 109 114 public void store(BeehiveWsTypeMetadata objectModel) 115 throws IOException 116 { 117 OutputStream os = env.getFiler().createBinaryFile(Filer.Location.CLASS_TREE, "", getLocation(objectModel.getClassName())); 118 ObjectOutputStream oos = null; 119 try { 120 if (null == os) { 121 throw new IOException ("cannot persist object model without output stream"); 122 } 123 if (null == objectModel) { 124 throw new IOException ("cannot persist empty object model "); 125 } 126 oos = new ObjectOutputStream (os); 127 oos.writeObject(objectModel); 128 129 if (! isMarked) { 130 env.getFiler().createBinaryFile(Filer.Location.CLASS_TREE, "", new java.io.File (LOCATOR)); 131 isMarked = true; 132 } 133 } 134 finally { 135 if (null != oos) { 136 oos.flush(); 137 oos.close(); 138 } 139 } 140 } 141 142 151 public static Class loadWebServiceClass(String className) 152 throws ClassNotFoundException 153 { 154 Class clazz = null; 155 ClassLoader cl = Jsr181ObjectModelStore.class.getClassLoader(); 156 157 try { 159 clazz = cl.loadClass(className); 160 } 161 162 catch (ClassNotFoundException e) { 164 final File wsDirectory = new File (new File (cl.getResource(LOCATOR).getFile()).getParent()); 165 String fqClassName = getFullyQualifiedClassName(wsDirectory, className); 166 if (null == fqClassName) { 168 fqClassName = getFullyQualifiedClassName(wsDirectory, className.substring(0, className.length() - "Service".length())); 169 } 170 clazz = cl.loadClass(fqClassName); 171 } 172 return clazz; 173 } 174 175 187 private static String getFullyQualifiedClassName(final File wsDirectory, final String simpleName) 188 throws ClassNotFoundException 189 { 190 FilenameFilter filenameFilter = new FilenameFilter () { 191 public boolean accept(File dir, String name) { 192 return (wsDirectory.equals(dir) && (name.endsWith(simpleName + EXTENSION))); 193 } 194 }; 195 String fqClassName = null; 196 for (File file : wsDirectory.listFiles(filenameFilter)) { 197 String filename = file.getName(); 198 if (filename.endsWith(EXTENSION)) { 199 fqClassName = filename.substring(0, filename.length() - EXTENSION.length()); 200 break; 201 } 202 } 203 return fqClassName; 204 } 205 } 206 | Popular Tags |