KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > plugins > security > ModelPlugin


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.plugins.security;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.jboss.portal.common.FQN;
26 import org.jboss.portal.common.metadata.MetaData;
27 import org.jboss.portal.core.metadata.ItemMetaData;
28 import org.jboss.portal.core.metadata.ModelMetaData;
29 import org.jboss.portal.core.metadata.PermissionDescriptionMetaData;
30 import org.jboss.portal.core.metadata.PermissionMetaData;
31 import org.jboss.portal.core.metadata.SchemeMetaData;
32 import org.jboss.portal.core.security.Item;
33 import org.jboss.portal.core.security.Model;
34 import org.jboss.portal.core.security.PermissionDescription;
35 import org.jboss.portal.core.security.Scheme;
36 import org.jboss.portal.core.security.SchemeStore;
37 import org.jboss.portal.core.modules.SecurityModule;
38 import org.jboss.portal.core.modules.ModuleConstants;
39 import org.jboss.portal.server.Application;
40 import org.jboss.portal.server.Component;
41 import org.jboss.portal.server.kernel.Service;
42 import org.jboss.portal.server.kernel.ServiceImplementation;
43 import org.jboss.portal.server.plugins.PluginService;
44
45 /**
46  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
47  * @version $Revision: 1.10 $
48  */

49 public class ModelPlugin extends PluginService implements Model
50 {
51
52    private static final Logger log = Logger.getLogger(ModelPlugin.class);
53
54    /** Our meta data. */
55    private ModelMetaData metaData;
56
57    /** The content provider, may be null if not defined. */
58    private ModelContentProvider contentProvider;
59
60    /** The component we are attached to. */
61    private Component component;
62
63    /** The description of all permissions. */
64    private Map JavaDoc permissionDescriptions;
65
66    /** The map that contains the permission implications. */
67    private Map JavaDoc impliedMap;
68
69    /** The default scheme. */
70    private Scheme defaultScheme;
71
72    /** The security module. */
73    private SecurityModule module;
74
75    public ModelPlugin()
76    {
77       permissionDescriptions = new HashMap JavaDoc();
78       impliedMap = new HashMap JavaDoc();
79       defaultScheme = new Scheme();
80    }
81
82    public void create() throws Exception JavaDoc
83    {
84       //
85
String JavaDoc contentProviderClass = metaData.getContentProviderClass();
86       if (contentProviderClass != null)
87       {
88          Application app = component.getApplication();
89          ClassLoader JavaDoc loader = app.getClassLoader();
90          try
91          {
92             Class JavaDoc clazz = loader.loadClass(contentProviderClass);
93             contentProvider = (ModelContentProvider)clazz.newInstance();
94          }
95          catch (ClassNotFoundException JavaDoc e)
96          {
97             log.error("Class not found for model content provider = " + contentProviderClass);
98          }
99          catch (InstantiationException JavaDoc e)
100          {
101             log.error("Cannot create instance of model content provider = " + contentProviderClass);
102          }
103          catch (IllegalAccessException JavaDoc e)
104          {
105             log.error("Cannot create instance of model content provider = " + contentProviderClass);
106          }
107       }
108
109       // Put initial data
110
for (Iterator JavaDoc i = metaData.getPermissionDescriptions().iterator();i.hasNext();)
111       {
112          PermissionDescriptionMetaData permissionDescriptionMD = (PermissionDescriptionMetaData)i.next();
113
114          String JavaDoc name = permissionDescriptionMD.getName();
115          String JavaDoc description = permissionDescriptionMD.getDescription();
116          
117          PermissionDescription permissionDescription = new PermissionDescription(name, description);
118          permissionDescriptions.put(name, permissionDescription);
119          impliedMap.put(name, new HashSet JavaDoc(permissionDescriptionMD.getImpliedSet()));
120       }
121       
122       // Compute transitive closure
123
for (Iterator JavaDoc i = impliedMap.entrySet().iterator();i.hasNext();)
124       {
125          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
126          String JavaDoc source = (String JavaDoc)entry.getKey();
127          Set JavaDoc targets = (Set JavaDoc)entry.getValue();
128          
129          //
130
while (true)
131          {
132             // Detect cycles
133
if (targets.contains(source))
134             {
135                throw new Exception JavaDoc("Not an acyclic graph");
136             }
137             int size = targets.size();
138             for (Iterator JavaDoc j = new HashSet JavaDoc(targets).iterator();j.hasNext();)
139             {
140                String JavaDoc target = (String JavaDoc)j.next();
141                Set JavaDoc targetTargets = (Set JavaDoc)impliedMap.get(target);
142                
143                // Detect missing target
144
if (targetTargets == null)
145                {
146                   throw new Exception JavaDoc("Missing permission " + target);
147                }
148                targets.addAll(targetTargets);
149             }
150             if (size == targets.size())
151             {
152                break;
153             }
154          }
155       }
156
157       defaultScheme = createScheme(metaData.getScheme());
158    }
159
160    public static Scheme createScheme(SchemeMetaData schemeMD) throws Exception JavaDoc
161    {
162       Scheme scheme = new Scheme();
163       final Map JavaDoc pathToRolesToPermissions = new HashMap JavaDoc();
164       for (Iterator JavaDoc i = schemeMD.getItems().iterator();i.hasNext();)
165       {
166          ItemMetaData itemMD = (ItemMetaData)i.next();
167          String JavaDoc path = itemMD.getPath();
168          if (path.charAt(0) != '/')
169          {
170             // Not good, should begin with /
171
throw new Exception JavaDoc("Invalid path " + path);
172          }
173          List JavaDoc tmp = new ArrayList JavaDoc();
174          int index = path.indexOf('/');
175          while (index != -1)
176          {
177             String JavaDoc s = path.substring(0, index);
178             if (s.length() > 0)
179             {
180                tmp.add(s);
181             }
182             path = path.substring(index + 1);
183             index = path.indexOf('/');
184          }
185          FQN fqn = new FQN((String JavaDoc[])tmp.toArray(new String JavaDoc[tmp.size()]));
186          Map JavaDoc roleNameToPermissionNames = new HashMap JavaDoc();
187          for (Iterator JavaDoc j = itemMD.getPermissions().iterator();j.hasNext();)
188          {
189             PermissionMetaData permissionMD = (PermissionMetaData)j.next();
190             String JavaDoc permissionName = permissionMD.getName();
191             Set JavaDoc roleNames = permissionMD.getRoleNames();
192             for (Iterator JavaDoc k = roleNames.iterator();k.hasNext();)
193             {
194                String JavaDoc roleName = (String JavaDoc)k.next();
195                Set JavaDoc permissionNames = (Set JavaDoc)roleNameToPermissionNames.get(roleName);
196                if (permissionNames == null)
197                {
198                   permissionNames = new HashSet JavaDoc();
199                   roleNameToPermissionNames.put(roleName, permissionNames);
200                }
201                permissionNames.add(permissionName);
202                scheme.add((String JavaDoc[])tmp.toArray(new String JavaDoc[tmp.size()]), roleName, permissionName);
203             }
204          }
205          pathToRolesToPermissions.put(fqn, roleNameToPermissionNames);
206       }
207       return scheme;
208    }
209
210    public void start() throws Exception JavaDoc
211    {
212       if (contentProvider != null)
213       {
214          try
215          {
216             Application app = component.getApplication();
217             ServletContext JavaDoc ctx = app.getServletContext();
218             contentProvider.init(ctx);
219          }
220          catch (Exception JavaDoc e)
221          {
222             log.error("Error during content model provider initialialization", e);
223          }
224       }
225    }
226
227    public void stop()
228    {
229       if (contentProvider != null)
230       {
231          try
232          {
233             contentProvider.destroy();
234          }
235          catch (Exception JavaDoc e)
236          {
237             log.error("Error during content model provider destructio", e);
238          }
239       }
240    }
241
242    public void destroy()
243    {
244       permissionDescriptions.clear();
245       impliedMap.clear();
246       defaultScheme = null;
247       contentProvider = null;
248    }
249    
250    public Item getItem(String JavaDoc[] path)
251    {
252       if (contentProvider == null)
253       {
254          return null;
255       }
256       else
257       {
258          return contentProvider.getItem(path);
259       }
260    }
261
262    public Iterator JavaDoc getChildren(String JavaDoc[] path)
263    {
264       if (contentProvider == null)
265       {
266          return null;
267       }
268       else
269       {
270          return contentProvider.getChildren(path);
271       }
272    }
273
274    public Map JavaDoc getPermissionDescriptions()
275    {
276       return Collections.unmodifiableMap(permissionDescriptions);
277    }
278
279    public boolean implies(String JavaDoc sourcePermission, String JavaDoc targetPermission)
280    {
281       if (sourcePermission == null)
282       {
283          throw new IllegalArgumentException JavaDoc("source must not be null");
284       }
285       if (targetPermission == null)
286       {
287          throw new IllegalArgumentException JavaDoc("target must not be null");
288       }
289       if (sourcePermission.equals(targetPermission))
290       {
291          return true;
292       }
293       Set JavaDoc impliedSet = (Set JavaDoc)impliedMap.get(sourcePermission);
294       if (impliedSet == null)
295       {
296          return false;
297       }
298       boolean implies = impliedSet.contains(targetPermission);
299       return implies;
300    }
301
302    public Scheme getDefaultScheme()
303    {
304       return defaultScheme;
305    }
306    
307    public void setMetaData(MetaData metaData)
308    {
309       this.metaData = (ModelMetaData)metaData;
310    }
311
312    public MetaData getMetaData()
313    {
314       return metaData;
315    }
316
317    //
318

319    public void addIDependOn(ServiceImplementation implementation)
320    {
321       Service service = implementation.getService();
322       if (service instanceof Component)
323       {
324          component = (Component)service;
325       }
326    }
327
328    public void removeIDependOn(ServiceImplementation implementation)
329    {
330       Service service = implementation.getService();
331       if (service instanceof Component)
332       {
333          component = null;
334       }
335    }
336
337    public SchemeStore getSchemeStore()
338    {
339       if (module == null)
340       {
341          try
342          {
343             module = (SecurityModule)new InitialContext JavaDoc().lookup(ModuleConstants.SECURITYMODULE_JNDINAME);
344          }
345          catch (NamingException JavaDoc e)
346          {
347             log.error("Cannot get security module", e);
348          }
349       }
350       return module.getStore(component.getID());
351    }
352 }
353
Popular Tags