KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > repository > J2EEResourceCollectionImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.repository;
24
25 import java.util.*;
26
27 /**
28  * Concrete implementation class for J2EEResourceCollection.
29  *
30  * @author Kenneth Saks
31  */

32 public class J2EEResourceCollectionImpl implements J2EEResourceCollection {
33
34     private Map resourceSets_;
35
36     public J2EEResourceCollectionImpl() {
37         resourceSets_ = new HashMap();
38     }
39
40     public Set getResourcesByType(int type) {
41         Map resourceSet = getResourcesInternal(type);
42         Collection collection = resourceSet.values();
43         Set shallowCopy = new HashSet();
44         for(Iterator iter = collection.iterator(); iter.hasNext();) {
45             J2EEResource next = (J2EEResource) iter.next();
46             shallowCopy.add(next);
47         }
48         return shallowCopy;
49     }
50     
51     public Set getAllResources() {
52         Set allResources = new HashSet();
53         Collection resourcesByType = resourceSets_.values();
54         for(Iterator iter = resourcesByType.iterator(); iter.hasNext(); ) {
55             HashMap next = (HashMap) iter.next();
56             allResources.addAll(next.values());
57         }
58         return allResources;
59     }
60
61     public void addResource(J2EEResource resource) {
62         Map resourceSet = getResourcesInternal(resource.getType());
63         resourceSet.put(resource.getName(), resource);
64     }
65
66     public boolean removeResource(J2EEResource resource) {
67         Map resourceSet = getResourcesInternal(resource.getType());
68         boolean removed =
69             (resourceSet.remove(resource.getName()) != null);
70         return removed;
71     }
72
73     public void removeAllResourcesByType(int type) {
74         resourceSets_.remove(new Integer JavaDoc(type));
75     }
76
77     public J2EEResource getResourceByName(int type, String JavaDoc resourceName) {
78         Map resourceSet = getResourcesInternal(type);
79         return (J2EEResource) resourceSet.get(resourceName);
80             
81     }
82
83     private Map getResourcesInternal(int type) {
84         Map resourceSet = (Map) resourceSets_.get(new Integer JavaDoc(type));
85         if( resourceSet == null ) {
86             resourceSet = new HashMap();
87             resourceSets_.put(new Integer JavaDoc(type), resourceSet);
88         }
89         return resourceSet;
90     }
91
92 }
93
Popular Tags