KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmiimpl > mof > model > NamespaceImpl


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 package org.netbeans.jmiimpl.mof.model;
20
21 import java.util.*;
22 import javax.jmi.model.*;
23 import javax.jmi.reflect.*;
24
25 import org.netbeans.mdr.util.*;
26 import org.netbeans.mdr.storagemodel.*;
27 import org.netbeans.mdr.persistence.*;
28 import org.netbeans.mdr.handlers.*;
29
30 /**
31  * Implements MOF operations
32  *
33  * @author mmatula
34  */

35 public abstract class NamespaceImpl extends ModelElementImpl implements Namespace {
36
37     protected NamespaceImpl(StorableObject storable) {
38         super(storable);
39     }
40
41     private static final HashMap contains = new HashMap();
42
43     public static void clearContains() {
44         contains.clear();
45     }
46
47     protected static StorableAssociation resolveContains(StorableFeatured storable) throws StorageException {
48         StorableAssociation result = (StorableAssociation) contains.get(storable.getOutermostPackageId());
49
50         if (result == null) {
51             result = storable.getClassProxy().getReferenceDescriptor(MOFConstants.SH_MODEL_MODEL_ELEMENT_CONTAINER).getAssociation();
52             contains.put(storable.getOutermostPackageId(), result);
53         }
54
55         return result;
56     }
57
58     // --- operations
59

60     public ModelElement lookupElement(String JavaDoc name) throws NameNotFoundException {
61         _lock(false);
62         try {
63             Collection contents;
64
65     // StorableFeatured storable = (StorableFeatured) _getDelegate();
66
// Collection items = storable.getMdrStorage().objectsFromAdditionalIndex(storable.getOutermostPackageId(), "ModelElement.name", name);
67
//
68
// if (items != null) {
69
// try {
70
// StorableAssociation assoc = resolveContains(storable);
71
// contents = (Collection) assoc.queryObjects(assoc.getEnd1Name(), storable.getMofId());
72
// } catch (StorageException e) {
73
// throw Logger.getDefault().annotate(new DebugException(), e);
74
// }
75
// Object result;
76
// for (Iterator it = items.iterator(); it.hasNext();) {
77
// result = it.next();
78
// if (contents.contains(result)) {
79
// return (ModelElement) BaseObjectHandler.getHandler((StorableBaseObject) result);
80
// }
81
// }
82
// } else {
83
contents = getContents();
84
85                 for(Iterator it = contents.iterator(); it.hasNext();) {
86                     ModelElement el = (ModelElement) it.next();
87                     if (el.getName().equals(name)) {
88                         return el;
89                     }
90                 }
91     // }
92
} finally {
93             _unlock();
94         }
95
96         throw new NameNotFoundException(name);
97     }
98
99     public ModelElement resolveQualifiedName(List qualifiedName) throws NameNotResolvedException {
100         if (qualifiedName.size() < 1) {
101             throw new DebugException("No qualified name provided.");
102         }
103         
104         ModelElement element;
105         Iterator qnIterator = qualifiedName.iterator();
106         String JavaDoc name = (String JavaDoc) qnIterator.next();
107         ArrayList rest = new ArrayList();
108
109         _lock(false);
110         try {
111             try {
112                 element = lookupElement(name);
113             } catch (NameNotFoundException e) {
114                 throw new NameNotResolvedException("InvalidName", qualifiedName);
115             }
116
117             for (; qnIterator.hasNext();) {
118                 rest.add(qnIterator.next());
119             }
120
121             if (rest.size() >= 1) {
122                 if (element instanceof Namespace) {
123                     element = ((Namespace) element).resolveQualifiedName(rest);
124                 } else {
125                     throw new NameNotResolvedException("NotNameSpace", qualifiedName);
126                 }
127             }
128         } finally {
129             _unlock();
130         }
131
132         return element;
133     }
134
135     public boolean nameIsValid(String JavaDoc proposedName) {
136         _lock(false);
137         try {
138             if (this instanceof GeneralizableElement) {
139                 ((GeneralizableElement) this).lookupElementExtended(proposedName);
140             } else {
141                 this.lookupElement(proposedName);
142             }
143             return false;
144         } catch (NameNotFoundException e) {
145             return true;
146         } finally {
147             _unlock();
148         }
149    }
150
151     public List findElementsByType(javax.jmi.model.MofClass ofType, boolean includeSubtypes ) {
152         _lock(false);
153         try {
154             ArrayList result = new ArrayList();
155             Collection contents = getContents();
156             RefObject element;
157
158             for (Iterator it = contents.iterator(); it.hasNext();) {
159                 element = (RefObject) it.next();
160                 if (element.refIsInstanceOf(ofType, includeSubtypes)) {
161                     result.add(element);
162                 }
163             }
164
165             return result;
166         } finally {
167             _unlock();
168         }
169     }
170
171     // --- derived attributes
172

173 }
174
Popular Tags