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:
042: package org.netbeans.modules.compapp.javaee.sunresources.tool.cmap;
043:
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.Iterator;
047:
048: /**
049: * @author echou
050: *
051: */
052: public class CMap {
053:
054: private String name;
055:
056: /* contains list of nodes in the CMap, entry in map
057: * <Class, CMapNode>, key is "qualified classname"
058: */
059: private HashMap<String, CMapNode> nodes = new HashMap<String, CMapNode>();
060:
061: /* contains list of resource nodes in the CMap, entry in map
062: * <String, ResourceNode>, key is "resType+resJndiName"
063: */
064: private HashMap<String, ResourceNode> resNodes = new HashMap<String, ResourceNode>();
065:
066: public CMap(String name) {
067: this .name = name;
068: }
069:
070: /**
071: * @param cls
072: * @return
073: */
074: public boolean hasNode(String clsName) {
075: return nodes.containsKey(clsName);
076: }
077:
078: /**
079: * @param cls
080: */
081: public CMapNode findNode(String clsName) {
082: return nodes.get(clsName);
083: }
084:
085: /**
086: * @param cls
087: * @param node
088: */
089: public void addNode(String clsName, CMapNode node) {
090: nodes.put(clsName, node);
091: }
092:
093: public Iterator<CMapNode> getNodes() {
094: return nodes.values().iterator();
095: }
096:
097: public ResourceNode findResNode(ResourceDepend resDepend) {
098: return resNodes.get(resDepend.getTargetResType() + "+" + // NOI18N
099: resDepend.getTargetResJndiName());
100: }
101:
102: public ResourceNode findResNodeByLogicalName(String logicalName) {
103: for (Iterator<ResourceNode> iter = resNodes.values().iterator(); iter
104: .hasNext();) {
105: ResourceNode resNode = iter.next();
106: if (resNode.getLogicalName().equals(logicalName)) {
107: return resNode;
108: }
109: }
110: return null;
111: }
112:
113: public void addResNode(ResourceNode resNode) {
114: resNodes.put(resNode.getResType() + "+"
115: + resNode.getResJndiName(), // NOI18N
116: resNode);
117: }
118:
119: public Iterator<ResourceNode> getResNodes() {
120: return resNodes.values().iterator();
121: }
122:
123: public void postProcess() throws Exception {
124: // find all the target node for EJB dependency
125: for (Iterator<CMapNode> iter = getNodes(); iter.hasNext();) {
126: CMapNode node = iter.next();
127: ArrayList<EJBDepend> ejbDepends = node.getEjbDepends();
128: for (int i = 0; i < ejbDepends.size(); i++) {
129: EJBDepend ejbDepend = ejbDepends.get(i);
130: CMapNode targetNode = findEJBNodeImplIntf(ejbDepend
131: .getTargetIntfName());
132: ejbDepend.setTarget(targetNode);
133: }
134: }
135:
136: // find all the target node for Resource dependency, if none exists,
137: // create one, since resource nodes are abstract
138: for (Iterator<CMapNode> iter = getNodes(); iter.hasNext();) {
139: CMapNode node = iter.next();
140: ArrayList<ResourceDepend> resDepends = node.getResDepends();
141: for (int i = 0; i < resDepends.size(); i++) {
142: ResourceDepend resDepend = resDepends.get(i);
143: ResourceNode resNode = findResNode(resDepend);
144: if (resNode == null) {
145: String logicalName;
146: if (resDepend.getMappedName() == null
147: || resDepend.getMappedName().equals("")) { // NOI18N
148: logicalName = resDepend.getTargetResJndiName();
149: } else {
150: logicalName = resDepend.getMappedName();
151: }
152: resNode = new ResourceNode(logicalName, resDepend
153: .getTargetResType(), resDepend
154: .getTargetResJndiName(), resDepend
155: .getType(), resDepend.getProps());
156: addResNode(resNode);
157: }
158: resDepend.setTarget(resNode);
159: }
160: }
161:
162: // after all the Resource nodes are created, we need to find out
163: // which resource node is each MDB listening to
164: for (Iterator<CMapNode> iter = getNodes(); iter.hasNext();) {
165: CMapNode node = iter.next();
166: if (node instanceof MDBNode) {
167: MDBNode mdbNode = (MDBNode) node;
168: ResourceNode resNode = findResNodeByLogicalName(mdbNode
169: .getMappedName());
170: if (resNode == null) {
171: // if the resource node does not exist, then create it
172: resNode = new ResourceNode(mdbNode.getMappedName(),
173: null, null, ResourceNode.ResourceType.JMS,
174: null);
175: addResNode(resNode);
176: }
177: mdbNode.setTargetListenNode(resNode);
178: }
179: }
180: }
181:
182: private EJBNode findEJBNodeImplIntf(String targetIntfName) {
183: for (Iterator<CMapNode> iter = getNodes(); iter.hasNext();) {
184: CMapNode node = iter.next();
185: if (node instanceof EJBNode) {
186: EJBNode ejbNode = (EJBNode) node;
187: if (ejbNode.implements Interface(targetIntfName)) {
188: return ejbNode;
189: }
190: }
191: }
192: return null;
193: }
194:
195: @Override
196: public String toString() {
197: StringBuffer sb = new StringBuffer();
198: sb.append("CMap: " + name + "\n"); // NOI18N
199: for (Iterator<CMapNode> iter = nodes.values().iterator(); iter
200: .hasNext();) {
201: sb.append("Node: " + iter.next() + "\n"); // NOI18N
202: }
203: for (Iterator<ResourceNode> iter = resNodes.values().iterator(); iter
204: .hasNext();) {
205: sb.append("Node: " + iter.next() + "\n"); // NOI18N
206: }
207: return sb.toString();
208: }
209:
210: }
|