KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > SchemaElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 import java.io.*;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.*;
25
26 import org.netbeans.modules.dbschema.util.*;
27
28 import org.netbeans.modules.dbschema.migration.archiver.*;
29
30 /** Describes an entire database schema.
31  */

32 public class SchemaElement extends DBElement {
33     /** Status when the schema element is not yet prepared. */
34     public static final int STATUS_NOT = 0;
35
36     /** Status when the schema element contains unrecoverable errors. */
37     public static final int STATUS_ERROR = 1;
38
39     /** Status when the schema element contains minor errors. */
40     public static final int STATUS_PARTIAL = 2;
41
42     /** Status when the schema element has been parsed and is error-free. */
43     public static final int STATUS_OK = 3;
44
45     /** Version of the database schema API. */
46     public static final int CURRENT_VERSION_NO = 2;
47     private int versionNo;
48
49     /** Creates a new schema element represented in memory.
50      */

51     public SchemaElement() {
52         this(new Memory());
53     }
54
55     /** Creates a new schema element.
56      * @param impl the pluggable implementation
57      */

58     public SchemaElement(Impl impl) {
59         super(impl);
60     }
61
62     /** Returns the implementation for the schema.
63      * @return implementation for the schema
64      */

65     final Impl getSchemaImpl() {
66         return (Impl)getElementImpl();
67     }
68
69     /** Returns if the schema is compatible with current API version.
70      * @return true if schema is compatible; false otherwise
71      */

72     public boolean isCompatibleVersion() {
73         return (getVersionNo() == CURRENT_VERSION_NO);
74     }
75     
76     /** Getter for property versionNo.
77      * @return Value of property versionNo.
78      */

79     public int getVersionNo() {
80         return versionNo;
81     }
82     
83     /** Setter for property versionNo.
84      * @param versionNo New value of property versionNo.
85      */

86     public void setVersionNo(int versionNo) {
87         this.versionNo = versionNo;
88     }
89
90     /** Cache for read schemas. */
91     protected static Map schemaCache = new HashMap();
92     
93     /** Last used schema. */
94     private static SchemaElement lastSchema;
95     
96     /** Returns the last used schema.
97      * @return the last used schema
98      */

99     protected static SchemaElement getLastSchema() {
100         return lastSchema;
101     }
102     
103     /** Sets the last used schema.
104      * @param last the last used schema
105      */

106     protected static void setLastSchema(SchemaElement last) {
107         lastSchema = last;
108     }
109     
110     /** Removes the specified schema from cache.
111      * @param name the schema to remove
112      */

113     public static void removeFromCache(String JavaDoc name) {
114         synchronized (schemaCache) {
115             if (getLastSchema() != null)
116                 if (getLastSchema().getName().getFullName().equals(name))
117                     setLastSchema(null);
118             
119             schemaCache.remove(name);
120         }
121     }
122     
123     /** Adds the specified schema element to cache.
124      * @param schema the schema element to add
125      */

126     public static void addToCache(SchemaElement schema) {
127         synchronized (schemaCache) {
128             schemaCache.put(schema.getName().getFullName(), schema);
129             SchemaElement.setLastSchema(schema);
130         }
131     }
132     
133     /** Returns the SchemaElement object associated with the schema with
134      * the given string name and object. The second argument is meant to
135      * help define the context for loading of the schema and can be a
136      * FileObject[] or FileObject for use in the IDE or a ClassLoader for
137      * use at runtime. Note that if if FileObject[] is used, the first match
138      * is returned if it's not already in the cache. It might be extended
139      * later to accept a Project as well. Any other non-null value for the
140      * second argument will result in an UnsupportedOperationException.
141      * @param name the schema name
142      * @param obj the schema context
143      * @return the SchemaElement object for the given schema name
144      */

145     public static SchemaElement forName(String JavaDoc name, Object JavaDoc obj) {
146         if (IDEUtil.isIDERunning())
147             return SchemaElementUtil.forName(name, obj);
148  
149         if (obj == null)
150             return forNameInternal(name, SchemaElement.class.getClassLoader());
151         if (obj instanceof ClassLoader JavaDoc)
152             return forNameInternal(name, (ClassLoader JavaDoc)obj);
153
154         // if we got to this point the second object is not null, the
155
// IDE is not running, and the type of object is not one we can
156
// handle
157
throw new UnsupportedOperationException JavaDoc("Cannot lookup schema " +
158             name + " in context of type " + obj.getClass() +
159             " expected ClassLoader or null.");
160     }
161
162     /** Returns the SchemaElement object associated with the schema with the given string name, loaded by the given classloader.
163      * @param name the schema name
164      * @param cl the schema classloader
165      * @return the SchemaElement object for the given schema name
166      */

167     private static SchemaElement forNameInternal(String JavaDoc name, ClassLoader JavaDoc cl) {
168         SchemaElement se = getLastSchema();
169   
170         if (se != null && se.getName().getFullName().equals(name))
171             return se;
172         else
173             synchronized (schemaCache) {
174                 se = (SchemaElement) schemaCache.get(name);
175                 if (se != null)
176                     return se;
177                 
178                 InputStream is = cl.getResourceAsStream(NameUtil.getSchemaResourceName(name));
179
180                 if (is != null)
181                     try {
182                         ObjectInput i = new XMLInputStream(is);
183                         se = (SchemaElement) i.readObject();
184                         if (!se.isCompatibleVersion()) {
185                             String JavaDoc message = MessageFormat.format(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("PreviousVersion"), new String JavaDoc[] {name}); //NOI18N
186
System.out.println(message);
187                         }
188                         i.close();
189
190                         se.setName(DBIdentifier.create(name));
191
192                         SchemaElement.addToCache(se);
193                         
194                         // MBO: now set the declaring schema in TableElement(transient field)
195
TableElement tables[] = se.getTables();
196                         int size = (tables != null) ? tables.length : 0;
197                         for (int j = 0; j < size; j++)
198                             tables[j].setDeclaringSchema(se);
199                     } catch (Exception JavaDoc exc) {
200                         if (Boolean.getBoolean("netbeans.debug.exceptions")) // NOI18N
201
System.out.println(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("SchemaNotFound")); //NOI18N
202
}
203                 else
204                     if (Boolean.getBoolean("netbeans.debug.exceptions")) // NOI18N
205
System.out.println(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("SchemaNotFound")); //NOI18N
206

207                 return se;
208             }
209     }
210
211     /** Returns the SchemaElement object associated with the schema with the given string name.
212      * @param name the schema name
213      * @return the SchemaElement object for the given schema name
214      */

215     public static SchemaElement forName(String JavaDoc name) {
216         return forName(name, null);
217     }
218
219     /** Gets the parsing status of the element.
220      * This is a non-blocking operation.
221      * @return one of {@link #STATUS_NOT}, {@link #STATUS_ERROR}, {@link
222      * #STATUS_PARTIAL}, or {@link #STATUS_OK}
223      */

224     public int getStatus() {
225         return getSchemaImpl().getStatus();
226     }
227
228     /** Sets the schema name of this schema snapshot.
229      * @param id the schema name, or <code>null</code>
230      * @exception DBException if the operation cannot proceed
231      */

232     public void setSchema(DBIdentifier schema) throws DBException {
233         getSchemaImpl().setSchema(schema);
234     }
235
236     /** Gets the schema name of this schema snapshot.
237      * @return the schema name, or <code>null</code> if this snapshot does not
238      * have a schema name
239      */

240     public DBIdentifier getSchema() {
241         return getSchemaImpl().getSchema();
242     }
243
244     /** Sets the catalog name of this schema snapshot.
245      * @param id the catalog name, or <code>null</code>
246      * @exception DBException if the operation cannot proceed
247      */

248     public void setCatalog(DBIdentifier catalog) throws DBException {
249         getSchemaImpl().setCatalog(catalog);
250     }
251
252     /** Gets the catalog name of this schema snapshot.
253      * @return the catalog name, or <code>null</code> if this snapshot does
254      * not have a catalog name
255      */

256     public DBIdentifier getCatalog() {
257         return getSchemaImpl().getCatalog();
258     }
259
260     /** Adds a new table to the schema snapshot.
261      * @param el the table to add
262      * @throws DBException if impossible
263      */

264     public void addTable(TableElement el) throws DBException {
265         addTables(new TableElement[] {el});
266     }
267
268     /** Adds some new tables to the schema snapshot.
269      * @param els the tables to add
270      * @throws DBException if impossible
271      */

272     public void addTables(final TableElement[] els) throws DBException {
273         for (int i = 0; i < els.length; i++) {
274             if (getTable(els[i].getName()) != null)
275                 throwAddException("FMT_EXC_AddTable", els[i]); //NOI18N
276
if (els[i].getDeclaringSchema() == null)
277                 els[i].setDeclaringSchema(this);
278         }
279
280         getSchemaImpl().changeTables(els, Impl.ADD);
281     }
282
283     /** Removes a table from the schema snapshot.
284      * @param el the table to remove
285      * @throws DBException if impossible
286      */

287     public void removeTable(TableElement el) throws DBException {
288         removeTables(new TableElement[] {el});
289     }
290
291     /** Removes some tables from the schema snapshot.
292      * @param els the columns to remove
293      * @throws DBException if impossible
294      */

295     public void removeTables(final TableElement[] els) throws DBException {
296         getSchemaImpl().changeTables(els, Impl.REMOVE);
297     }
298
299     /** Sets the tables for this schema snapshot.
300      * Previous tables are removed.
301      * @param els the new tables
302      * @throws DBException if impossible
303      */

304     public void setTables(TableElement[] els) throws DBException {
305         if (els == null)
306             throw new NullPointerException JavaDoc(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("NullTables")); //NOI18N
307

308         getSchemaImpl().changeTables(els, Impl.SET);
309     }
310
311     /** Gets all tables in this schema snapshot.
312      * @return the tables
313      */

314     public TableElement[] getTables() {
315         return getSchemaImpl().getTables();
316     }
317
318     /** Finds a table by name.
319      * @param name the name of the table for which to look
320      * @return the element or <code>null</code> if not found
321      */

322     public TableElement getTable(DBIdentifier name) {
323         return getSchemaImpl().getTable(name);
324     }
325
326     /** This method just throws localized exception. It is used during
327      * adding class element, which already exists in source.
328      * @param formatKey The message format key to localized bundle.
329      * @param element The element which can't be added
330      * @exception DBException is alway thrown from this method.
331      */

332     private void throwAddException(String JavaDoc formatKey, TableElement element) throws DBException {
333         //MessageFormat format = new MessageFormat(ElementFormat.bundle.getString(formatKey));
334
String JavaDoc msg = /*format.format(new Object[] { */element.getName().getName();// });
335
throw new DBException(msg);
336     }
337
338     /** Getter for property url.
339      * @return Value of property url.
340      */

341     public String JavaDoc getUrl() {
342         return getSchemaImpl().getUrl();
343     }
344
345     /** Setter for property url.
346      * @param url New value of property url.
347      */

348     public void setUrl(String JavaDoc url) throws DBException {
349             getSchemaImpl().setUrl(url);
350     }
351
352     /** Getter for property username.
353      * @return Value of property username.
354      */

355     public String JavaDoc getUsername() {
356         return getSchemaImpl().getUsername();
357     }
358
359     /** Setter for property username.
360      * @param username New value of property username.
361      */

362     public void setUsername(String JavaDoc username) throws DBException {
363         getSchemaImpl().setUsername(username);
364     }
365
366     /** Getter for property driver.
367      * @return Value of property driver.
368      */

369     public String JavaDoc getDriver() {
370         return getSchemaImpl().getDriver();
371     }
372
373     /** Setter for property driver.
374      * @param driver New value of property driver.
375      */

376     public void setDriver(String JavaDoc driver) {
377         getSchemaImpl().setDriver(driver);
378     }
379
380     /** Getter for property databaseProductName.
381      * @return Value of property databaseProductName.
382      */

383     public String JavaDoc getDatabaseProductName() {
384         return getSchemaImpl().getDatabaseProductName();
385     }
386
387     /** Setter for property databaseProductName.
388      * @param databaseProductName New value of property databaseProductName.
389      */

390     public void setDatabaseProductName(String JavaDoc databaseProductName) throws DBException {
391         getSchemaImpl().setDatabaseProductName(databaseProductName);
392     }
393
394     /** Getter for property databaseProductVersion.
395      * @return Value of property databaseProductVersion.
396      */

397     public String JavaDoc getDatabaseProductVersion() {
398         return getSchemaImpl().getDatabaseProductVersion();
399     }
400
401     /** Setter for property databaseProductVersion.
402      * @param databaseProductVersion New value of property databaseProductVersion.
403      */

404     public void setDatabaseProductVersion(String JavaDoc databaseProductVersion) throws DBException {
405         getSchemaImpl().setDatabaseProductVersion(databaseProductVersion);
406     }
407
408     /** Getter for property driverName.
409      * @return Value of property driverName.
410      */

411     public String JavaDoc getDriverName() {
412         return getSchemaImpl().getDriverName();
413     }
414
415     /** Setter for property driverName.
416      * @param driverName New value of property driverName.
417      */

418     public void setDriverName(String JavaDoc driverName) throws DBException {
419         getSchemaImpl().setDriverName(driverName);
420     }
421
422     /** Getter for property driverVersion.
423      * @return Value of property driverVersion.
424      */

425     public String JavaDoc getDriverVersion() {
426         return getSchemaImpl().getDriverVersion();
427     }
428
429     /** Setter for property driverVersion.
430      * @param driverVersion New value of property driverVersion.
431      */

432     public void setDriverVersion(String JavaDoc driverVersion) throws DBException {
433         getSchemaImpl().setDriverVersion(driverVersion);
434     }
435
436     /** Saves the current schema to an XML file.
437      * @param filename the system-dependent filename
438      */

439     public void save(String JavaDoc filename) {
440         setVersionNo(CURRENT_VERSION_NO);
441         
442         try {
443             OutputStream s = new FileOutputStream(filename);
444             ObjectOutput o = new XMLOutputStream(s);
445             o.writeObject(this);
446             o.close();
447         }
448         catch (Exception JavaDoc e) {
449             e.printStackTrace();
450         }
451     }
452     
453     /** Saves the current schema to an XML file.
454      * @param s the output stream
455      */

456     public void save(OutputStream s) {
457         setVersionNo(CURRENT_VERSION_NO);
458         
459         try {
460             ObjectOutput o = new XMLOutputStream(s);
461             o.writeObject(this);
462             o.close();
463         }
464         catch (Exception JavaDoc e) {
465             e.printStackTrace();
466         }
467     }
468
469     /** Pluggable behaviour for schema elements.
470      * @see SchemaElement
471      */

472     public static interface Impl extends DBElement.Impl {
473         /** Gets the parsing status of the element.
474         * This is a non-blocking operation.
475         * @return one of {@link #STATUS_NOT}, {@link #STATUS_ERROR},
476         * {@link #STATUS_PARTIAL}, or {@link #STATUS_OK}
477         */

478         public int getStatus();
479
480         /** Sets the schema name of this schema snapshot.
481          * @param id the schema name, or <code>null</code>
482          * @exception DBException if the operation cannot proceed
483          */

484         public void setSchema(DBIdentifier id) throws DBException;
485
486         /** Get the schema name of this schema snapshot.
487          * @return the schema name, or <code>null</code> if this snapshot does
488          * not have a schema name
489          */

490         public DBIdentifier getSchema();
491
492         /** Sets the catalog name of this schema snapshot.
493          * @param id the catalog name, or <code>null</code>
494          * @exception DBException if the operation cannot proceed
495          */

496         public void setCatalog(DBIdentifier id) throws DBException;
497
498         /** Gets the catalog name of this schema snapshot.
499          * @return the catalog name, or <code>null</code> if this snapshot does
500          * not have a catalog name
501          */

502         public DBIdentifier getCatalog();
503
504         /** Change the set of tables.
505         * @param elems the tables to change
506         * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
507         * @exception DBException if the action cannot be handled
508         */

509         public void changeTables(TableElement[] elems, int action) throws DBException;
510
511         /** Gets all tables.
512         * @return the tables
513         */

514         public TableElement[] getTables();
515
516         /** Finds a table by name.
517         * @param name the name for which to look
518         * @return the table, or <code>null</code> if it does not exist
519         */

520         public TableElement getTable(DBIdentifier name);
521
522         /** Getter for property url.
523          * @return Value of property url.
524          */

525         public String JavaDoc getUrl();
526
527         /** Setter for property url.
528          * @param url New value of property url.
529          */

530         public void setUrl(String JavaDoc url) throws DBException;
531
532         /** Getter for property username.
533          * @return Value of property username.
534          */

535         public String JavaDoc getUsername();
536
537         /** Setter for property username.
538          * @param username New value of property username.
539          */

540         public void setUsername(String JavaDoc username) throws DBException;
541
542         /** Getter for property driver.
543          * @return Value of property driver.
544          */

545         public String JavaDoc getDriver();
546
547         /** Setter for property driver.
548          * @param driver New value of property driver.
549          */

550         public void setDriver(String JavaDoc driver);
551
552         /** Getter for property databaseProductName.
553          * @return Value of property databaseProductName.
554          */

555         public String JavaDoc getDatabaseProductName();
556
557         /** Setter for property databaseProductName.
558          * @param databaseProductName New value of property databaseProductName.
559          */

560         public void setDatabaseProductName(String JavaDoc databaseProductName) throws DBException;
561
562         /** Getter for property databaseProductVersion.
563          * @return Value of property databaseProductVersion.
564          */

565         public String JavaDoc getDatabaseProductVersion();
566
567         /** Setter for property databaseProductVersion.
568          * @param databaseProductVersion New value of property databaseProductVersion.
569          */

570         public void setDatabaseProductVersion(String JavaDoc databaseProductVersion) throws DBException;
571
572         /** Getter for property driverName.
573          * @return Value of property driverName.
574          */

575         public String JavaDoc getDriverName();
576
577         /** Setter for property driverName.
578          * @param driverName New value of property driverName.
579          */

580         public void setDriverName(String JavaDoc driverName) throws DBException;
581
582         /** Getter for property driverVersion.
583          * @return Value of property driverVersion.
584          */

585         public String JavaDoc getDriverVersion();
586
587         /** Setter for property driverVersion.
588          * @param driverVersion New value of property driverVersion.
589          */

590         public void setDriverVersion(String JavaDoc driverVersion) throws DBException;
591     }
592
593     /** Memory based implementation of the element factory.
594      */

595     static final class Memory extends DBElement.Memory implements Impl {
596         /** collection of tables */
597         private DBMemoryCollection.Table tables;
598
599         private DBIdentifier _catalog;
600         private DBIdentifier _schema;
601         private int _status;
602         private String JavaDoc _url;
603         private String JavaDoc _username;
604         private String JavaDoc _driver;
605         private String JavaDoc _databaseProductName;
606         private String JavaDoc _databaseProductVersion;
607         private String JavaDoc _driverName;
608         private String JavaDoc _driverVersion;
609
610         /** Default constructor
611          */

612         public Memory() {
613             super();
614         }
615
616         /** Copy constructor.
617          * @param el element to copy from
618          */

619         public Memory(SchemaElement el) {
620             super(el);
621             _catalog = el.getCatalog();
622             _schema = el.getSchema();
623             _status = el.getStatus();
624             _url = el.getUrl();
625             _username = el.getUsername();
626             _driver = el.getDriver();
627             _databaseProductName = el.getDatabaseProductName();
628             _databaseProductVersion = el.getDatabaseProductVersion();
629             _driverName = el.getDriverName();
630             _driverVersion = el.getDriverVersion();
631         }
632
633         /** Late initialization of initialization of copy elements.
634          */

635         public void copyFrom(SchemaElement copyFrom) throws DBException {
636             changeTables(copyFrom.getTables(), SET);
637         }
638
639         /** Changes set of elements.
640          * @param elems elements to change
641          * @exception SourceException if the action cannot be handled
642          */

643         public synchronized void changeTables(TableElement[] elems, int action) throws DBException {
644             initTables();
645             tables.change(elems, action);
646         }
647
648         /** Gets all tables.
649          * @return the tables
650          */

651         public synchronized TableElement[] getTables() {
652             initTables();
653             return (TableElement[]) tables.getElements();
654         }
655
656         /** Finds a table with given name.
657          * @param name the name of table for which to look
658          * @return the element or null if table with such name does not exist
659          */

660         public synchronized TableElement getTable(DBIdentifier name) {
661             initTables();
662             return (TableElement) tables.getElement(name);
663         }
664
665         /** Initializes the collection of tables.
666          */

667         void initTables() {
668             if (tables == null)
669                 tables = new DBMemoryCollection.Table(this);
670         }
671
672         /** Getter for the associated schema
673          * @return the schema element for this impl
674          */

675         final SchemaElement getSchemaElement() {
676             return (SchemaElement) _element;
677         }
678
679         /** Gets the parsing status of the element.
680          * This is a non-blocking operation.
681          * @return one of {@link #STATUS_NOT}, {@link #STATUS_ERROR}, {@link
682          * #STATUS_PARTIAL}, or {@link #STATUS_OK}
683          */

684         public int getStatus() {
685             return _status;
686         }
687
688         public void setSchema(DBIdentifier id) throws DBException {
689             DBIdentifier old = _schema;
690
691             _schema = id;
692             firePropertyChange (PROP_SCHEMA, old, id);
693         }
694
695         public DBIdentifier getSchema() {
696             if (_schema == null) // lazy initialization !?
697
_schema = DBIdentifier.create(""); //NOI18N
698

699             return _schema;
700         }
701
702         public void setCatalog(DBIdentifier id) throws DBException {
703             DBIdentifier old = _catalog;
704
705             _catalog = id;
706             firePropertyChange (PROP_CATALOG, old, id);
707         }
708
709         public DBIdentifier getCatalog() {
710             if (_catalog == null) // lazy initialization !?
711
_catalog = DBIdentifier.create(""); //NOI18N
712

713             return _catalog;
714         }
715
716         public String JavaDoc getUrl() {
717             return _url;
718         }
719
720         public void setUrl(String JavaDoc url) throws DBException{
721             _url = url;
722         }
723
724         public String JavaDoc getUsername() {
725             return _username;
726         }
727
728         public void setUsername(String JavaDoc username) throws DBException{
729             _username = username;
730         }
731
732         public String JavaDoc getDriver() {
733             return _driverName;
734         }
735
736         public void setDriver(String JavaDoc driver){
737             _driver = driver;
738         }
739
740         public String JavaDoc getDatabaseProductName() {
741             return _databaseProductName;
742         }
743
744         public void setDatabaseProductName(String JavaDoc databaseProductName) throws DBException {
745             _databaseProductName = databaseProductName;
746         }
747
748         public String JavaDoc getDatabaseProductVersion() {
749             return _databaseProductVersion;
750         }
751
752         public void setDatabaseProductVersion(String JavaDoc databaseProductVersion) throws DBException{
753             _databaseProductVersion = databaseProductVersion;
754         }
755
756         public String JavaDoc getDriverName() {
757             return _driverName;
758         }
759
760         public void setDriverName(String JavaDoc driverName) throws DBException {
761             _driverName = driverName;
762         }
763
764         public String JavaDoc getDriverVersion() {
765             return _driverVersion;
766         }
767
768         public void setDriverVersion(String JavaDoc driverVersion) throws DBException {
769             _driverVersion = driverVersion;
770         }
771     }
772 }
773
Popular Tags