KickJava   Java API By Example, From Geeks To Geeks.

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


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.handlers.*;
28 import org.netbeans.mdr.persistence.StorageException;
29
30 /**
31  * Implements MOF operations of GeneralizableElement class of MOF model.
32  *
33  * @author Martin Matula
34  */

35 public abstract class GeneralizableElementImpl extends NamespaceImpl implements GeneralizableElement {
36
37     protected GeneralizableElementImpl(StorableObject storable) {
38         super(storable);
39     }
40
41     private Collection/*<StorableFeatured>*/ extendedNamespace() {
42         try {
43             StorableFeatured storable = (StorableFeatured) _getDelegate();
44             StorableAssociation assoc = resolveContains(storable);
45             Collection result = new ArrayList((Collection) assoc.queryObjects(assoc.getEnd1Name(), storable.getMofId()));
46
47             if (this instanceof GeneralizableElement) {
48                 Collection supertypes = ((GeneralizableElement) this).allSupertypes();
49
50                 for (Iterator it = supertypes.iterator(); it.hasNext();) {
51                     storable = (StorableFeatured) ((BaseObjectHandler) it.next())._getDelegate();
52                     result.addAll((Collection) assoc.queryObjects(assoc.getEnd1Name(), storable.getMofId()));
53                 }
54             }
55
56             if (this instanceof MofPackage) {
57                 ArrayList imports = new ArrayList();
58                 Object JavaDoc temp;
59
60                 for (Iterator it = result.iterator(); it.hasNext();) {
61                     temp = it.next();
62                     if (temp instanceof Import) {
63                         imports.add(((BaseObjectHandler) ((Import) temp).getImportedNamespace())._getDelegate());
64                     }
65                 }
66
67                 result.addAll(imports);
68             }
69
70             return result;
71         } catch (StorageException e) {
72             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
73         }
74     }
75
76     private void allSupertypes2(List result, Set visited) {
77         List tempElements = getSupertypes();
78         GeneralizableElementImpl element;
79
80         for (Iterator it = tempElements.iterator(); it.hasNext();) {
81             element = (GeneralizableElementImpl) it.next();
82             if (visited.add(element)) {
83                 element.allSupertypes2(result, visited);
84                 result.add(element);
85             }
86         }
87     }
88
89     // --- operations
90

91     public List allSupertypes() {
92         _lock(false);
93         try {
94             List result = new ArrayList();
95             allSupertypes2(result, new HashSet());
96             return result;
97         } finally {
98             _unlock();
99         }
100     }
101
102     public ModelElement lookupElementExtended(String JavaDoc name) throws NameNotFoundException {
103         _lock(false);
104         try {
105             Collection contents = extendedNamespace();
106     // StorableBaseObject storable = _getDelegate();
107
// Collection items = storable.getMdrStorage().objectsFromAdditionalIndex(storable.getOutermostPackageId(), "ModelElement.name", name);
108
//
109
// if (items != null) {
110
// Object result;
111
// for (Iterator it = items.iterator(); it.hasNext();) {
112
// result = it.next();
113
// if (contents.contains(result)) {
114
// return (ModelElement) BaseObjectHandler.getHandler((StorableBaseObject) result);
115
// }
116
// }
117
// } else {
118
for (Iterator it = contents.iterator(); it.hasNext();) {
119                     ModelElement el = (ModelElement) _getRepository().getHandler((StorableBaseObject) it.next());
120                     if (el.getName().equals(name)) {
121                         return el;
122                     }
123                 }
124     // }
125
} finally {
126             _unlock();
127         }
128
129         throw new NameNotFoundException(name);
130     }
131
132     public List findElementsByTypeExtended(javax.jmi.model.MofClass ofType, boolean includeSubtypes) {
133         _lock(false);
134         try {
135             ArrayList result = new ArrayList();
136             RefObject element;
137             Collection contents = extendedNamespace();
138
139             for (Iterator it = contents.iterator(); it.hasNext();) {
140                 element = (RefObject) _getRepository().getHandler((StorableObject) it.next());
141                 if (element.refIsInstanceOf(ofType, includeSubtypes)) {
142                     result.add(element);
143                 }
144             }
145
146             return result;
147         } finally {
148             _unlock();
149         }
150     }
151
152     // --- derived attributes
153

154 }
155
Popular Tags