KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > model > jsr181 > Jsr181ObjectModelStore


1 package org.apache.beehive.wsm.model.jsr181;
2
3 /*
4  * Copyright 2005 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$Factory
19  */

20
21 import java.io.File JavaDoc;
22 import java.io.FilenameFilter JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28
29 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
30 import com.sun.mirror.apt.Filer;
31
32 import java.net.URL JavaDoc;
33
34 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata;
35
36 /**
37  * Encapsulates all knowledge about where and how object models are persisted.
38  */

39 public class Jsr181ObjectModelStore {
40     
41     private final static String JavaDoc EXTENSION = ".ser";
42     private final static String JavaDoc LOCATOR = ".webservices";
43
44     private static boolean isMarked = false;
45     
46     private AnnotationProcessorEnvironment env;
47     
48     /**
49      * Constructor.
50      * @param env A context that is required to access the filer.
51      * TODO needs to be abstracted further.
52      */

53     public Jsr181ObjectModelStore(AnnotationProcessorEnvironment env) {
54         if (null == env) {
55             throw new IllegalArgumentException JavaDoc("illegal apt environment: <null>");
56         }
57         this.env = env;
58     }
59     
60     /**
61      * Encapsulates the encoding of the pathname for object models.
62      * @param className
63      * @return File
64      */

65     public static File JavaDoc getLocation(String JavaDoc className) {
66         return new File JavaDoc(className + EXTENSION);
67     }
68
69     /**
70      * @param clazz
71      * @throws IOException
72      * @throws ClassNotFoundException
73      */

74     public static BeehiveWsTypeMetadata load(Class JavaDoc clazz)
75             throws IOException JavaDoc, ClassNotFoundException JavaDoc
76     {
77         String JavaDoc resourceName = getLocation(clazz.getName()).toString();
78         URL JavaDoc url = clazz.getClassLoader().getResource(resourceName);
79         return load(url.openStream());
80     }
81     
82     /**
83      * @param is
84      * @throws IOException
85      * @throws ClassNotFoundException
86      */

87     public static BeehiveWsTypeMetadata load(InputStream JavaDoc is)
88             throws IOException JavaDoc, ClassNotFoundException JavaDoc
89     {
90         Jsr181TypeMetadataImpl objectModel = null;
91         ObjectInputStream JavaDoc ois = null;
92
93         try {
94             if (null == is) {
95                 throw new IOException JavaDoc("cannot load object model without input stream");
96             }
97             ois = new ObjectInputStream JavaDoc(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     /**
110      * Persists a given <code>objectModel</code>.
111      * @param objectModel The object mode to be persisted.
112      * @throws IOException If the object model cannot be persisted.
113      */

114     public void store(BeehiveWsTypeMetadata objectModel)
115             throws IOException JavaDoc
116     {
117         OutputStream JavaDoc os = env.getFiler().createBinaryFile(Filer.Location.CLASS_TREE, "", getLocation(objectModel.getClassName()));
118         ObjectOutputStream JavaDoc oos = null;
119         try {
120             if (null == os) {
121                 throw new IOException JavaDoc("cannot persist object model without output stream");
122             }
123             if (null == objectModel) {
124                 throw new IOException JavaDoc("cannot persist empty object model ");
125             }
126             oos = new ObjectOutputStream JavaDoc(os);
127             oos.writeObject(objectModel);
128             
129             if (! isMarked) {
130                 env.getFiler().createBinaryFile(Filer.Location.CLASS_TREE, "", new java.io.File JavaDoc(LOCATOR));
131                 isMarked = true;
132             }
133         }
134         finally {
135             if (null != oos) {
136                 oos.flush();
137                 oos.close();
138             }
139         }
140     }
141
142     /**
143      * Tries to find the class with a given <code>className</code>. If
144      * <code>className</code> is a simple name, the method tries to find a
145      * matching fully qualified class name. The respective class is then
146      * loaded and returned.
147      * @param className A class name; may be a fully qualified or a simple class
148      * name.
149      * @return A class with a matching name.
150      */

151     public static Class JavaDoc loadWebServiceClass(String JavaDoc className)
152             throws ClassNotFoundException JavaDoc
153     {
154         Class JavaDoc clazz = null;
155         ClassLoader JavaDoc cl = Jsr181ObjectModelStore.class.getClassLoader();
156
157         // try className as fully qualified class name
158
try {
159             clazz = cl.loadClass(className);
160         }
161         
162         // try className as simple class name
163
catch (ClassNotFoundException JavaDoc e) {
164             final File JavaDoc wsDirectory = new File JavaDoc(new File JavaDoc(cl.getResource(LOCATOR).getFile()).getParent());
165             String JavaDoc fqClassName = getFullyQualifiedClassName(wsDirectory, className);
166             // todo: we do this for the TCK only...
167
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     /**
176      * Implements the algorithm for finding a class given a <code>simpleName
177      * </code>: searches the directory used for persisting object models for
178      * matching names.
179      * TODO currently returns the first match; would an exception be the better
180      * approach?
181      * @param wsDirectory The directory to be searched.
182      * @param simpleName A simple name for the class.
183      * @return The fully qualified class name if it can be found.
184      * @throws ClassNotFoundException If a matching fully qualified class name
185      * cannot be found.
186      */

187     private static String JavaDoc getFullyQualifiedClassName(final File JavaDoc wsDirectory, final String JavaDoc simpleName)
188             throws ClassNotFoundException JavaDoc
189     {
190         FilenameFilter JavaDoc filenameFilter = new FilenameFilter JavaDoc() {
191             public boolean accept(File JavaDoc dir, String JavaDoc name) {
192                 return (wsDirectory.equals(dir) && (name.endsWith(simpleName + EXTENSION)));
193             }
194         };
195         String JavaDoc fqClassName = null;
196         for (File JavaDoc file : wsDirectory.listFiles(filenameFilter)) {
197             String JavaDoc 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