KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > DefaultResourceType


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.policyframework;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import com.sslexplorer.security.SessionInfo;
29
30 /**
31  * Default implementation of a
32  * {@link com.sslexplorer.policyframework.ResourceType}.
33  *
34  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
35  * @see com.sslexplorer.policyframework.PolicyConstants
36  */

37 public class DefaultResourceType implements ResourceType, Serializable JavaDoc {
38
39     // Private instance variables
40

41     private int id;
42     private String JavaDoc bundle;
43     private List JavaDoc<Permission> permissions;
44     private String JavaDoc permissionClass;
45     private boolean policyRequired;
46
47     /**
48      * Constructor for resource types. If the permission class is
49      * {@link PolicyConstants#DELEGATION_CLASS} then {@link #isPolicyRequired()}
50      * will return <code>true</code>, all other classes will return
51      * <code>false</code>
52      *
53      * @param id unique ID of the resource type
54      * @param bundle bundle that contains the title / description keys
55      * @param permissionClass permission class
56      */

57     public DefaultResourceType(int id, String JavaDoc bundle, String JavaDoc permissionClass) {
58         this(id, bundle, permissionClass, PolicyConstants.DELEGATION_CLASS.equals(permissionClass));
59     }
60
61     /**
62      * Constructor
63      *
64      * @param id unique ID of the resource type
65      * @param bundle bundle that contains the title / description keys
66      * @param permissionClass permission class
67      * @param policyRequired <code>true</code> if this resource type must be
68      * attached to a policy
69      */

70     public DefaultResourceType(int id, String JavaDoc bundle, String JavaDoc permissionClass, boolean policyRequired) {
71         this.id = id;
72         this.bundle = bundle;
73         this.permissionClass = permissionClass;
74         permissions = new ArrayList JavaDoc<Permission>();
75         this.policyRequired = policyRequired;
76
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see com.sslexplorer.boot.policyframework.ResourceType#getPermissionClass()
83      */

84     public String JavaDoc getPermissionClass() {
85         return permissionClass;
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see com.sslexplorer.boot.policyframework.ResourceType#setPermissionClass(java.lang.String)
92      */

93     public void setPermissionClass(String JavaDoc permissionClass) {
94         this.permissionClass = permissionClass;
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see com.sslexplorer.boot.policyframework.ResourceType#getResourceTypeId()
101      */

102     public int getResourceTypeId() {
103         return id;
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see com.sslexplorer.boot.policyframework.ResourceType#getBundle()
110      */

111     public String JavaDoc getBundle() {
112         return bundle;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see com.sslexplorer.boot.policyframework.ResourceType#getPermissions()
119      */

120     public Collection JavaDoc<Permission> getPermissions() {
121         return permissions;
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see com.sslexplorer.boot.policyframework.ResourceType#addPermission(com.sslexplorer.boot.policyframework.Permission)
128      */

129     public void addPermission(Permission permission) {
130         permissions.add(permission);
131     }
132
133     /*
134      * (non-Javadoc)
135      *
136      * @see com.sslexplorer.boot.policyframework.ResourceType#getPermission(int)
137      */

138     public Permission getPermission(int id) {
139         Permission p = null;
140         for (Iterator JavaDoc i = permissions.iterator(); i.hasNext();) {
141             p = (Permission) i.next();
142             if (p.getId() == id) {
143                 return p;
144             }
145         }
146         return null;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see java.lang.Object#equals(java.lang.Object)
153      */

154     public boolean equals(Object JavaDoc o) {
155         return o instanceof ResourceType && getResourceTypeId() == ((ResourceType) o).getResourceTypeId();
156     }
157
158     /**
159      * Compare two resources types using the ID
160      *
161      * @param o other resource type
162      * @return comparison
163      */

164     public int compareTo(ResourceType o) {
165         int i = getPermissionClass().compareTo(o.getPermissionClass());
166         return i == 0 ? new Integer JavaDoc(getResourceTypeId()).compareTo(new Integer JavaDoc(o.getResourceTypeId())) : i;
167     }
168
169     /**
170      * Return a string representation of this resource type. Only really used
171      * for debugging.
172      *
173      * @return string representation of this resource type.
174      * @see java.lang.Object#toString()
175      */

176     public String JavaDoc toString() {
177         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("ResourceType '");
178         buf.append(getResourceTypeId());
179         buf.append("' [permissionClass=");
180         buf.append(getPermissionClass());
181         buf.append(",bundle=");
182         buf.append(getBundle());
183         buf.append("]");
184         return buf.toString();
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see com.sslexplorer.boot.policyframework.ResourceType#getResourceById(int)
191      */

192     public Resource getResourceById(int resourceId) throws Exception JavaDoc {
193         throw new UnsupportedOperationException JavaDoc("Not supported.");
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see com.sslexplorer.policyframework.ResourceType#getResourceByName(java.lang.String,
200      * com.sslexplorer.security.SessionInfo)
201      */

202     public Resource getResourceByName(String JavaDoc resourceName, SessionInfo session) throws Exception JavaDoc {
203         throw new UnsupportedOperationException JavaDoc("Not supported.");
204     }
205
206     /*
207      * (non-Javadoc)
208      *
209      * @see com.sslexplorer.boot.policyframework.ResourceType#removeResource(int,
210      * com.sslexplorer.security.SessionInfo)
211      */

212     public Resource removeResource(int resourceId, SessionInfo session) throws Exception JavaDoc {
213         throw new UnsupportedOperationException JavaDoc("Not supported.");
214     }
215
216     /*
217      * (non-Javadoc)
218      *
219      * @see com.sslexplorer.boot.policyframework.ResourceType#updateResource(com.sslexplorer.boot.policyframework.Resource,
220      * com.sslexplorer.security.SessionInfo)
221      */

222     public void updateResource(Resource resource, SessionInfo session) throws Exception JavaDoc {
223         throw new UnsupportedOperationException JavaDoc("Not supported.");
224     }
225
226     /*
227      * (non-Javadoc)
228      *
229      * @see com.sslexplorer.policyframework.ResourceType#isPolicyRequired()
230      */

231     public boolean isPolicyRequired() {
232         return policyRequired;
233     }
234
235     /*
236      * (non-Javadoc)
237      *
238      * @see com.sslexplorer.policyframework.ResourceType#cloneResource(com.sslexplorer.policyframework.Resource)
239      */

240     public Resource cloneResource(Resource sourceResource, SessionInfo session) throws CloneNotSupportedException JavaDoc {
241         /* Because getting a resource should always return a new instance, this will
242          * will suffice as a clone operation
243          */

244         try {
245             Resource r = getResourceById(sourceResource.getResourceId());
246             int idx = 1;
247             while (true) {
248                 r.setResourceName((idx == 1 ? "Copy of " : ("Copy (" + idx + ") ")) + sourceResource.getResourceName());
249                 if (sourceResource.getResourceType().getResourceByName(r.getResourceName(), session) == null) {
250                     break;
251                 }
252                 idx++;
253             }
254             return r;
255         } catch (Exception JavaDoc e) {
256             e.printStackTrace();
257             throw new CloneNotSupportedException JavaDoc();
258         }
259     }
260 }
261
Popular Tags