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-2007 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.visualweb.dataconnectivity.naming;
042:
043: import java.lang.ref.Reference;
044: import java.lang.ref.WeakReference;
045: import java.util.Hashtable;
046: import java.util.Iterator;
047: import java.util.Locale;
048: import java.util.Map;
049: import java.util.ResourceBundle;
050: import java.util.Vector;
051: import javax.naming.Binding;
052: import javax.naming.CompositeName;
053: import javax.naming.Context;
054: import javax.naming.Name;
055: import javax.naming.NameClassPair;
056: import javax.naming.NameParser;
057: import javax.naming.NameNotFoundException;
058: import javax.naming.NamingEnumeration;
059: import javax.naming.NamingException;
060: import org.netbeans.api.project.Project;
061: import org.netbeans.modules.visualweb.dataconnectivity.model.DataSourceInfo;
062: import org.netbeans.modules.visualweb.dataconnectivity.sql.DesignTimeDataSourceHelper;
063: import org.openide.ErrorManager;
064:
065: /**
066: * Creator's naming context that is created per project
067: *
068: * @author John Kline, John Baker
069: */
070: class DesignTimeContext implements Context {
071: private Reference<Project> projectRef;
072: public static final String ROOT_CTX_TAG = "rootContext"; // NOI18N
073: public static final String CTX_TAG = "context"; // NOI18N
074: public static final String OBJ_TAG = "object"; // NOI18N
075: public static final String ARG_TAG = "arg"; // NOI18N
076: public static final String NAME_ATTR = "name"; // NOI18N
077: public static final String CLASS_ATTR = "class"; // NOI18N
078: public static final String VALUE_ATTR = "value"; // NOI18N
079: private static DesignTimeContext this Instance;
080: private Map bindings;
081: private static Hashtable env;
082: private boolean update = false;
083:
084: private static ResourceBundle rb = ResourceBundle
085: .getBundle(
086: "org.netbeans.modules.visualweb.dataconnectivity.naming.Bundle", // NOI18N
087: Locale.getDefault());
088:
089: // entry for subcontexts in a context's TreeMap (map)
090: private class Subcontext {
091: private String subcontextName;
092: private DesignTimeContext subcontext;
093:
094: Subcontext(String subcontextName, DesignTimeContext subcontext) {
095: this .subcontextName = subcontextName;
096: this .subcontext = subcontext;
097: }
098: }
099:
100: /** Creates a new instance of DesignTimeDatasourceContext */
101: private DesignTimeContext(Project p, Hashtable env) {
102: projectRef = new WeakReference<Project>(p);
103: this Instance = this ;
104: this Instance.env = new Hashtable(env);
105: }
106:
107: private static class DesignTimeContextHolder {
108: static final DesignTimeContext setDesignTimeContext(
109: Project prj, Hashtable environment) {
110: return new DesignTimeContext(prj, environment);
111: }
112: }
113:
114: public static DesignTimeContext getDesignTimeContext() {
115: return this Instance;
116: }
117:
118: public static DesignTimeContext createDesignTimeContext(
119: Project prj, Hashtable environment) {
120: DesignTimeContext dtCtx = null;
121:
122: if (this Instance != null) {
123: if (prj != null) {
124: if (!((Project) this Instance.projectRef.get())
125: .equals(prj)) {
126: dtCtx = DesignTimeContextHolder
127: .setDesignTimeContext(prj, environment);
128: } else {
129: dtCtx = getDesignTimeContext();
130: if (dtCtx == null) {
131: dtCtx = DesignTimeContextHolder
132: .setDesignTimeContext(prj, environment);
133: }
134: }
135: }
136: } else if (prj != null) {
137: dtCtx = DesignTimeContextHolder.setDesignTimeContext(prj,
138: environment);
139: }
140:
141: return dtCtx;
142: }
143:
144: public Object lookup(Name name) throws NamingException {
145: Log.getLogger().entering(getClass().getName(), "lookup", name); //NOI18N
146:
147: // Update datasources for current project
148: if (name.toString().contains("java:comp/env/jdbc") || !update) {
149: return updateBindings(projectRef, name);
150: }
151:
152: return null;
153: }
154:
155: public Object lookup(String name) throws NamingException {
156: return lookup(new CompositeName(name));
157: }
158:
159: public void bind(Name arg0, Object arg1) throws NamingException {
160: throw new UnsupportedOperationException("Not supported yet.");
161: }
162:
163: public void bind(String arg0, Object arg1) throws NamingException {
164: throw new UnsupportedOperationException("Not supported yet.");
165: }
166:
167: public void rebind(Name arg0, Object arg1) throws NamingException {
168: throw new UnsupportedOperationException("Not supported yet.");
169: }
170:
171: public void rebind(String arg0, Object arg1) throws NamingException {
172: throw new UnsupportedOperationException("Not supported yet.");
173: }
174:
175: public void unbind(Name arg0) throws NamingException {
176: throw new UnsupportedOperationException("Not supported yet.");
177: }
178:
179: public void unbind(String arg0) throws NamingException {
180: throw new UnsupportedOperationException("Not supported yet.");
181: }
182:
183: public void rename(Name arg0, Name arg1) throws NamingException {
184: throw new UnsupportedOperationException("Not supported yet.");
185: }
186:
187: public void rename(String arg0, String arg1) throws NamingException {
188: throw new UnsupportedOperationException("Not supported yet.");
189: }
190:
191: public NamingEnumeration<NameClassPair> list(Name arg0)
192: throws NamingException {
193: throw new UnsupportedOperationException("Not supported yet.");
194: }
195:
196: public NamingEnumeration<NameClassPair> list(String arg0)
197: throws NamingException {
198: throw new UnsupportedOperationException("Not supported yet.");
199: }
200:
201: public NamingEnumeration<Binding> listBindings(Name name)
202: throws NamingException {
203: Log.getLogger().entering(getClass().getName(), "listBindings",
204: name); //NOI18N
205: if (name.size() == 0) {
206: Vector v = new Vector();
207: for (Iterator i = bindings.keySet().iterator(); i.hasNext();) {
208: String key = (String) i.next();
209: Object obj = bindings.get(key);
210: if (obj instanceof Subcontext) {
211: obj = ((Subcontext) obj).subcontext;
212: }
213: v.add(new Binding(key, obj, true));
214: }
215: return new DesignTimeNamingEnumeration(v.elements());
216: } else {
217: Object obj = lookup(name);
218: if (!(obj instanceof Context)) {
219: throw new NameNotFoundException(name.toString());
220: } else {
221: return ((Context) obj)
222: .listBindings(new CompositeName());
223: }
224: }
225:
226: }
227:
228: public NamingEnumeration<Binding> listBindings(String name)
229: throws NamingException {
230: return listBindings(new CompositeName(name));
231: }
232:
233: public void destroySubcontext(Name arg0) throws NamingException {
234: throw new UnsupportedOperationException("Not supported yet.");
235: }
236:
237: public void destroySubcontext(String arg0) throws NamingException {
238: throw new UnsupportedOperationException("Not supported yet.");
239: }
240:
241: public Context createSubcontext(Name arg0) throws NamingException {
242: throw new UnsupportedOperationException("Not supported yet.");
243: }
244:
245: public Context createSubcontext(String arg0) throws NamingException {
246: throw new UnsupportedOperationException("Not supported yet.");
247: }
248:
249: public Object lookupLink(Name arg0) throws NamingException {
250: throw new UnsupportedOperationException("Not supported yet.");
251: }
252:
253: public Object lookupLink(String arg0) throws NamingException {
254: throw new UnsupportedOperationException("Not supported yet.");
255: }
256:
257: public NameParser getNameParser(Name arg0) throws NamingException {
258: throw new UnsupportedOperationException("Not supported yet.");
259: }
260:
261: public NameParser getNameParser(String arg0) throws NamingException {
262: throw new UnsupportedOperationException("Not supported yet.");
263: }
264:
265: public Name composeName(Name arg0, Name arg1)
266: throws NamingException {
267: throw new UnsupportedOperationException("Not supported yet.");
268: }
269:
270: public String composeName(String arg0, String arg1)
271: throws NamingException {
272: throw new UnsupportedOperationException("Not supported yet.");
273: }
274:
275: public Object addToEnvironment(String arg0, Object arg1)
276: throws NamingException {
277: throw new UnsupportedOperationException("Not supported yet.");
278: }
279:
280: public Object removeFromEnvironment(String arg0)
281: throws NamingException {
282: throw new UnsupportedOperationException("Not supported yet.");
283: }
284:
285: public Hashtable<?, ?> getEnvironment() throws NamingException {
286: throw new UnsupportedOperationException("Not supported yet.");
287: }
288:
289: public void close() throws NamingException {
290: // not used by visual web
291: }
292:
293: public String getNameInNamespace() throws NamingException {
294: throw new UnsupportedOperationException("Not supported yet.");
295: }
296:
297: // Store the datasource info in the Project
298: private Object updateBindings(Reference projectReference, Name name) {
299: DesignTimeDataSourceHelper dsHelper = null;
300: Object obj = null;
301: update = true;
302:
303: try {
304: dsHelper = new DesignTimeDataSourceHelper();
305:
306: if (dsHelper
307: .datasourcesInProject((Project) projectReference
308: .get())) {
309: bindings = dsHelper
310: .updateDataSource((Project) projectReference
311: .get());
312: confirmConnections(bindings, dsHelper);
313: return bindings.get(name.toString());
314: }
315:
316: } catch (NamingException ne) {
317: ErrorManager.getDefault().notify(ne);
318: }
319: return obj;
320: }
321:
322: /**
323: * Make sure connections are available to inform the user if they aren't
324: */
325: private void confirmConnections(Map bindings,
326: DesignTimeDataSourceHelper dsHelper) {
327: // Check if valid connection has been registered. If not then check if the
328: // project is a legacy project and post an alert for migration
329: DataSourceInfo dsInfo = null;
330: for (int i = 0; i < bindings.size(); i++) {
331: dsInfo = (DataSourceInfo) bindings.get(i);
332:
333: // check if driverclass is derby and if it hasn't been started then try to start derby
334: }
335: }
336:
337: }
|