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:
042: package org.netbeans.modules.dbschema;
043:
044: import java.io.InputStream;
045: import java.io.ObjectInput;
046: import java.text.MessageFormat;
047: import java.util.ResourceBundle;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: import org.openide.filesystems.FileObject;
052:
053: import org.netbeans.modules.dbschema.migration.archiver.XMLInputStream;
054:
055: import org.netbeans.modules.dbschema.util.NameUtil;
056: import org.openide.loaders.DataObjectNotFoundException;
057:
058: public class SchemaElementUtil {
059:
060: private static FileObject schemaFO = null;
061:
062: /** Returns the SchemaElement object associated with the schema with
063: * the given string name and object. The second argument is meant to
064: * help define the context for loading of the schema and can be a
065: * FileObject[] or FileObject. Note that if if FileObject[] is used,
066: * the first match is returned if it's not already in the cache.
067: * It might be extended later to accept a Project as well.
068: * Any other non-null value for the second argument will result in an
069: * UnsupportedOperationException.
070: * @param name the schema name
071: * @param obj the schema context
072: * @return the SchemaElement object for the given schema name
073: */
074: public static SchemaElement forName(String name, Object obj) {
075: SchemaElement se = SchemaElement.getLastSchema();
076:
077: if (se != null && se.getName().getFullName().equals(name)
078: && schemaFO == null)
079: return se;
080: else
081: synchronized (SchemaElement.schemaCache) {
082: String tempURL = ""; //NOI18N
083: if (schemaFO != null)
084: try {
085: tempURL = schemaFO.getURL().toString();
086: } catch (Exception exc) {
087: Logger.getLogger("global").log(Level.INFO,
088: null, exc);
089: }
090:
091: if (schemaFO == null)
092: se = (SchemaElement) SchemaElement.schemaCache
093: .get(name);
094: else
095: se = (SchemaElement) SchemaElement.schemaCache
096: .get(name + "#" + tempURL); //NOI18N
097: if (se != null)
098: return se;
099:
100: FileObject fo = null;
101: if (schemaFO == null) {
102: if (obj instanceof FileObject) {
103: fo = findResource((FileObject) obj, name);
104: } else if (obj instanceof FileObject[]) {
105: FileObject[] sourceRoots = (FileObject[]) obj;
106:
107: for (int i = 0; ((fo == null) && (i < sourceRoots.length)); i++) {
108: fo = findResource(sourceRoots[i], name);
109: }
110: } else if (obj != null) {
111: throw new UnsupportedOperationException(
112: "Cannot lookup schema "
113: + name
114: + " in context of type "
115: + obj.getClass()
116: + " expected FileObject, FileObject[], or null.");
117: }
118: } else
119: fo = schemaFO;
120: if (fo != null && fo.isValid()) {
121: try {
122: org.openide.loaders.DataObject dataObject = org.openide.loaders.DataObject
123: .find(fo);
124:
125: if (dataObject != null)
126: se = (SchemaElement) dataObject
127: .getCookie(SchemaElement.class);
128: } catch (ClassCastException e) {
129: // really ugly, caused by faulty code in DBSchemaDataObject.getCookie(...)
130: // just find it by unarchiving (below)
131: } catch (DataObjectNotFoundException e) {
132: Logger.getLogger("global").log(Level.INFO,
133: null, e);
134: // just find it by unarchiving (below)
135: }
136: if (se == null) {
137: try {
138: org.openide.awt.StatusDisplayer
139: .getDefault()
140: .setStatusText(
141: ResourceBundle
142: .getBundle(
143: "org.netbeans.modules.dbschema.resources.Bundle")
144: .getString(
145: "RetrievingSchema")); //NOI18N
146:
147: InputStream s = fo.getInputStream();
148: ObjectInput i = new XMLInputStream(s);
149: se = (SchemaElement) i.readObject();
150: if (!se.isCompatibleVersion()) {
151: String message = MessageFormat
152: .format(
153: ResourceBundle
154: .getBundle(
155: "org.netbeans.modules.dbschema.resources.Bundle")
156: .getString(
157: "PreviousVersion"),
158: new String[] { name }); //NOI18N
159: org.openide.DialogDisplayer
160: .getDefault()
161: .notify(
162: new org.openide.NotifyDescriptor.Message(
163: message,
164: org.openide.NotifyDescriptor.ERROR_MESSAGE));
165: }
166: i.close();
167:
168: se.setName(DBIdentifier.create(name));
169:
170: if (schemaFO == null)
171: SchemaElement.addToCache(se);
172: else {
173: SchemaElement.schemaCache.put(name
174: + "#" + tempURL, se); //NOI18N
175: SchemaElement.setLastSchema(se);
176: }
177:
178: // MBO: now set the declaring schema in TableElement(transient field)
179: TableElement tables[] = se.getTables();
180: int size = (tables != null) ? tables.length
181: : 0;
182: for (int j = 0; j < size; j++)
183: tables[j].setDeclaringSchema(se);
184:
185: } catch (Exception e) {
186: Logger.getLogger("global").log(Level.INFO,
187: null, e);
188: org.openide.awt.StatusDisplayer
189: .getDefault()
190: .setStatusText(
191: ResourceBundle
192: .getBundle(
193: "org.netbeans.modules.dbschema.resources.Bundle")
194: .getString(
195: "CannotRetrieve")); //NOI18N
196: }
197: }
198: } else
199: Logger
200: .getLogger("global")
201: .log(
202: Level.FINE,
203: ResourceBundle
204: .getBundle(
205: "org.netbeans.modules.dbschema.resources.Bundle")
206: .getString("SchemaNotFound")); //NOI18N
207:
208: return se;
209: }
210: }
211:
212: /** Returns the SchemaElement object associated with the schema with the given file object.
213: * @param fo the file object
214: * @return the SchemaElement object for the given file object
215: */
216: public static SchemaElement forName(FileObject fo) {
217: schemaFO = fo;
218: SchemaElement se = forName(fo.getName(), null);
219: schemaFO = null;
220:
221: return se;
222: }
223:
224: private static FileObject findResource(FileObject sourceRoot,
225: String name) {
226: // issue 119537: only look for dbschema files in the source root
227: return sourceRoot.getFileObject(NameUtil
228: .getSchemaResourceName(name));
229: }
230: }
|