001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.compapp.projects.common;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.FileNotFoundException;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.io.PrintWriter;
049: import java.io.StringWriter;
050: import java.util.ArrayList;
051: import java.util.Collections;
052: import java.util.List;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055: import org.netbeans.api.project.Project;
056: import org.openide.filesystems.FileLock;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059:
060: /**
061: * This class represents the in memory model that holds the entries of the wsdl/xsd
062: * imports used in resolving the implicit references in the xml files. It defines
063: * the interface to serialize/deserialize the entries to/from catalog.wsdl file to
064: * represent the model on disk as a wsdl model.
065: *
066: * @see CatalogWSDLModelSerializer
067: * @see ImplicitCatalogSupport
068: *
069: * @author chikkala
070: */
071: public class CatalogWSDL {
072:
073: /** catalog wsdl namespace */
074: public static final String TNS = "http://soa.netbeans.org/wsdl/catalog";
075: /** file name for the catalog wsdl */
076: public static final String CATALOG_WSDL_FILE = "catalog.wsdl";
077: /** name of the wsdl defintion */
078: public static final String CATALOG_NAME = "catalog";
079: /** logger */
080: private static final Logger sLogger = Logger
081: .getLogger(CatalogWSDL.class.getName());
082: /** serializer interface to read/write the catalog wsdl file */
083: private static CatalogWSDLSerializer sSerializer = new XAMCatalogWSDLSerializer();
084: /** entries in the catalog wsdl */
085: private List<Entry> mEntries;
086:
087: /**
088: * default constructor
089: */
090: public CatalogWSDL() {
091: this .mEntries = new ArrayList<Entry>();
092: }
093:
094: /**
095: * return the list of entries in the catalog model
096: * @return List of Entry objects
097: */
098: public List<Entry> getEntries() {
099: return Collections.unmodifiableList(this .mEntries);
100: }
101:
102: /**
103: * return the list of entries of type either wsdl or xsd imports
104: * @param type WSDL or XSD.
105: * @return List of Entry objects
106: */
107: public List<Entry> getEntries(EntryType type) {
108: List<Entry> entries = new ArrayList<Entry>();
109: for (Entry entry : this .mEntries) {
110: if (entry.getType().equals(type)) {
111: entries.add(entry);
112: }
113: }
114: return entries;
115: }
116:
117: /**
118: * return entries correpsonding to the namespace
119: * @param namespace namespace of the entreis
120: * @return List of Entry objects that has the namespace.
121: */
122: public List<Entry> getEntries(String namespace) {
123: List<Entry> entries = new ArrayList<Entry>();
124: for (Entry entry : this .mEntries) {
125: if (entry.getNamesapce().equals(namespace)) {
126: entries.add(entry);
127: }
128: }
129: return entries;
130: }
131:
132: /**
133: * finds the entry in the catalog.
134: * @param type type of the entry. can not be null
135: * @param namespace namespace. can not be null
136: * @param location location. can be null.
137: * @return Entry object
138: */
139: public Entry getEntry(EntryType type, String namespace,
140: String location) {
141: for (Entry entry : this .mEntries) {
142: if (entry.getType().equals(type)
143: && entry.getNamesapce().equals(namespace)
144: && (location == null || location.equals(entry
145: .getLocation()))) {
146: return entry;
147: }
148: }
149: return null;
150: }
151:
152: /**
153: * adds entries
154: * @param list of entries
155: * @return true if the entries are added else false.
156: */
157: public boolean addAllEntries(List<Entry> list) {
158: return this .mEntries.addAll(list);
159: }
160:
161: /**
162: * add single entrie
163: * @param entry entry to be added
164: * @return true if the entries is added else false.
165: */
166: public boolean addEntry(Entry entry) {
167: return this .mEntries.add(entry);
168: }
169:
170: /**
171: * removes the entry from the list
172: * @param entry object
173: * @return true if the entry is removed successfully
174: */
175: public boolean removeEntry(Entry entry) {
176: return this .mEntries.remove(entry);
177: }
178:
179: /**
180: * clear all entries
181: */
182: public void clear() {
183: this .mEntries.clear();
184: }
185:
186: /**
187: * set the static reference to the serializer that can be used
188: * to serialize/deserialze the model to/from file.
189: * @param serializer
190: */
191: public static void setCatalogWSDLSerializer(
192: CatalogWSDLSerializer serializer) {
193: sLogger.fine("new CatalogWSDLSerializer set");
194: sSerializer = serializer;
195: }
196:
197: /**
198: * return the current serializer. if not set, creates a default one and return.
199: * @return CatalogWSDLSerializer
200: */
201: public static CatalogWSDLSerializer getCatalogWSDLSerializer() {
202: if (sSerializer == null) {
203: sSerializer = new XAMCatalogWSDLSerializer();
204: }
205: return sSerializer;
206: }
207:
208: /**
209: * reads the catalog.xml file from a specified directory and return the
210: * in memory representation of it.
211: * @param fromDir directory in which to look for the catalog.wsdl file
212: * @return CatalogWSDL model
213: * @throws java.io.IOException on error
214: */
215: public static CatalogWSDL loadCatalogWSDL(FileObject fromDir)
216: throws IOException {
217: FileObject catFO = fromDir.getFileObject(CATALOG_WSDL_FILE);
218: if (catFO == null) {
219: throw new FileNotFoundException(
220: "Catalog.wsdl file not found in "
221: + fromDir.getPath());
222: }
223: return getCatalogWSDLSerializer().unmarshall(catFO);
224: }
225:
226: /**
227: * writes the in memory model to the catalog.wsdl file in a destination directory.
228: * creates the file if it is not existing.
229: * @param catalog in memory model
230: * @param destDir direcotry to save the model
231: * @throws java.io.IOException on error
232: */
233: public static void saveCatalogWSDL(CatalogWSDL catalog,
234: FileObject destDir) throws IOException {
235: FileObject catFO = CatalogWSDLSerializer.createCatalogWSDLFile(
236: destDir, true);
237: getCatalogWSDLSerializer().marshall(catalog, catFO);
238: }
239:
240: /**
241: * finds the catalog wsdl file in the project and return the corresponding
242: * file object.
243: *
244: * @param project project in which the catalog file should be looked up.
245: * @return file object correpsonding to the catalog wsdl file in the project
246: * @throws java.io.IOException on error
247: */
248: public static FileObject getCatalogWSDLFile(Project project)
249: throws IOException {
250: FileObject catFO = CatalogWSDLSerializer.createCatalogWSDLFile(
251: project.getProjectDirectory(), false);
252: return catFO;
253: }
254:
255: /**
256: * loads the catalog wsdl model from the catalog.wsdl file in a project
257: *
258: * @param project project from which to load the catalog wsdl model
259: * @return memory model of the catalog wsdl file
260: * @throws java.io.IOException on error.
261: */
262: public static CatalogWSDL loadCatalogWSDL(Project project)
263: throws IOException {
264: return loadCatalogWSDL(project.getProjectDirectory());
265: }
266:
267: /**
268: * saves the memory model of the catalog wsdl a location in a project.
269: * @param catalog memory model
270: * @param project project to which the model should be saved.
271: * @throws java.io.IOException on error.
272: */
273: public static void saveCatalogWSDL(CatalogWSDL catalog,
274: Project project) throws IOException {
275: saveCatalogWSDL(catalog, project.getProjectDirectory());
276: }
277:
278: /**
279: * enumeration that represents the supported entries to the catalog wsdl. only
280: * xsd and wsdl entries are supported.
281: */
282: public static enum EntryType {
283:
284: XSD, WSDL
285: }
286:
287: /**
288: * This class represents each entry into the catalog wsdl file in memory that
289: * correpsonds to the wsdl or xsd imports.
290: *
291: */
292: public static class Entry {
293:
294: /** type of entry. wsdl or xsd */
295: private EntryType mType;
296: /** namespce of the entry */
297: private String mNamespace;
298: /** location of the entry. location in case of wsdl import, schemaLocation
299: in case of xsd entry */
300: private String mLocation;
301:
302: /**
303: * constructor for the entry.
304: * @param type
305: */
306: public Entry(EntryType type) {
307: this .mType = type;
308: }
309:
310: /**
311: * getter for type
312: * @return EntryType
313: */
314: public EntryType getType() {
315: return this .mType;
316: }
317:
318: /**
319: *
320: * @return
321: */
322: public String getNamesapce() {
323: return mNamespace;
324: }
325:
326: /**
327: *
328: * @param namespace
329: */
330: public void setNamespace(String namespace) {
331: this .mNamespace = namespace;
332: }
333:
334: /**
335: *
336: * @return
337: */
338: public String getLocation() {
339: return this .mLocation;
340: }
341:
342: /**
343: *
344: * @param location
345: */
346: public void setLocation(String location) {
347: this .mLocation = location;
348: }
349:
350: @Override
351: public String toString() {
352: return " Type : " + this .mType + "\n Namespace: "
353: + this .mNamespace + "\n Location : "
354: + this .mLocation;
355: }
356:
357: /**
358: * creates a xsd entry
359: * @param namespace
360: * @param location
361: * @return
362: */
363: public static Entry createXSDEntry(String namespace,
364: String location) {
365: Entry entry = new Entry(EntryType.XSD);
366: entry.setNamespace(namespace);
367: entry.setLocation(location);
368: return entry;
369: }
370:
371: /**
372: * creates a wsdl entry
373: * @param namespace
374: * @param location
375: * @return
376: */
377: public static Entry createWSDLEntry(String namespace,
378: String location) {
379: Entry entry = new Entry(EntryType.WSDL);
380: entry.setNamespace(namespace);
381: entry.setLocation(location);
382: return entry;
383: }
384: }
385:
386: /**
387: * This class defines the serialization/deserialization interface that will
388: * be used to load/save memory model of the catalog wsdl file to/from the
389: * disk. Concrete implementation can use any technique like xdm model, dom model
390: * or other to implement the serialization/deserialization.
391: */
392: public static abstract class CatalogWSDLSerializer {
393:
394: /**
395: * called to save the catalog wsdl model to the file
396: * @param catWSDL model
397: * @param catFO file object
398: * @throws java.io.IOException on error
399: */
400: public abstract void marshall(CatalogWSDL catWSDL,
401: FileObject catFO) throws IOException;
402:
403: /**
404: * called to load the catalog wsdl model from the file
405: * @param catFO file object
406: * @return memory model of the catalog wsdl file
407: * @throws java.io.IOException on error.
408: */
409: public abstract CatalogWSDL unmarshall(FileObject catFO)
410: throws IOException;
411:
412: /**
413: * utility method to write a data to the file.
414: * @param outFO file object
415: * @param srcBuff buffer containing the data.
416: */
417: public static void saveToFileObject(FileObject outFO,
418: StringBuffer srcBuff) {
419: FileLock outLock = null;
420: OutputStream outS = null;
421: InputStream inS = null;
422:
423: try {
424: inS = new ByteArrayInputStream(srcBuff.toString()
425: .getBytes());
426: outLock = outFO.lock();
427: outS = outFO.getOutputStream(outLock);
428: FileUtil.copy(inS, outS);
429: } catch (Exception ex) {
430: ex.printStackTrace();
431: } finally {
432: if (inS != null) {
433: try {
434: inS.close();
435: } catch (IOException ex) {
436: sLogger.log(Level.FINER, ex.getMessage(), ex);
437: }
438: }
439: if (outS != null) {
440: try {
441: outS.close();
442: } catch (IOException ex) {
443: sLogger.log(Level.FINER, ex.getMessage(), ex);
444: }
445: }
446: if (outLock != null) {
447: outLock.releaseLock();
448: }
449: }
450: }
451:
452: /**
453: * utility method that can create the empty catalog wsdl file.
454: *
455: * @param destDir directory in which the catalog file will be created.
456: * @param overwrite overwrites with empty catalog wsdl defintion if the
457: * file existis if set to true.
458: * @return file object correpsonding to the catalog.wsdl file
459: * @throws java.io.IOException on error.
460: */
461: public static FileObject createCatalogWSDLFile(
462: FileObject destDir, boolean overwrite)
463: throws IOException {
464: FileObject fo = null;
465: fo = destDir.getFileObject(CATALOG_WSDL_FILE);
466:
467: if (fo != null && !overwrite) {
468: return fo;
469: }
470: if (fo == null) {
471: // create a new data
472: fo = FileUtil.createData(destDir, CATALOG_WSDL_FILE);
473: }
474: StringWriter writer = new StringWriter();
475: PrintWriter out = new PrintWriter(writer);
476: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
477: out.println("<definitions name=\"" + CATALOG_NAME
478: + "\" targetNamespace=\"" + TNS + "\"");
479: out
480: .println(" xmlns=\"http://schemas.xmlsoap.org/wsdl/\"");
481: out
482: .println(" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"");
483: out
484: .println(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
485: out.println(" xmlns:tns=\"" + TNS + "\"");
486: out.println(">");
487: out.println(" <types/>");
488: out.println("</definitions>");
489: out.close();
490: try {
491: writer.close();
492: } catch (IOException ioEx) {
493: //ignore.
494: }
495: saveToFileObject(fo, writer.getBuffer());
496:
497: return fo;
498: }
499: }
500: }
|