KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > filesystem > PermissionsManager


1 package com.openedit.users.filesystem;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6 import java.io.Reader JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.OpenEditRuntimeException;
15 import com.openedit.config.Configuration;
16 import com.openedit.config.XMLConfiguration;
17 import com.openedit.page.Page;
18 import com.openedit.page.manage.PageManager;
19 import com.openedit.users.Permission;
20 import com.openedit.util.XmlUtil;
21
22 public class PermissionsManager
23 {
24     protected List JavaDoc fieldSystemPermissionGroups;
25     protected PermissionGroup fieldUserPermissionGroup;
26     protected long fieldLastEditTime;
27     protected PageManager fieldPageManager;
28     protected File JavaDoc fieldRootDir;
29     
30     public PermissionsManager()
31     {
32     }
33     
34         
35     public void addSystemPermissions(Reader JavaDoc inReader)
36     {
37         PermissionGroup group = loadPermissions(inReader);
38         getSystemPermissionGroups().add(group);
39     }
40     
41     protected PermissionGroup loadPermissions(Reader JavaDoc inReader)
42     {
43         List JavaDoc permissions = new ArrayList JavaDoc();
44         XMLConfiguration userManagerConfig = new XMLConfiguration();
45         userManagerConfig.populate(new XmlUtil().getXml(inReader,"UTF-8"));
46
47         PermissionGroup pgroup = new PermissionGroup();
48         String JavaDoc name = userManagerConfig.getAttribute("name");
49         pgroup.setName(name);
50         
51         permissions = new ArrayList JavaDoc( );
52         
53         for (Iterator JavaDoc iter = userManagerConfig.getChildren("permission").iterator(); iter.hasNext();)
54         {
55             Configuration permissionElem = (Configuration)iter.next();
56             
57             String JavaDoc desc = permissionElem.getAttribute("description");
58             if ( desc == null)
59             {
60                 desc = permissionElem.getValue();
61             }
62             permissions.add( new Permission(permissionElem.getAttribute("id"),desc));
63         }
64         pgroup.setPermissions(permissions);
65         return pgroup;
66     }
67     
68     public PermissionGroup loadPermissions(Page inPage) throws OpenEditException
69     {
70         if( !inPage.exists())
71         {
72             return new PermissionGroup();
73         }
74         return loadPermissions(inPage.getReader());
75     }
76     
77     public List JavaDoc getSystemPermissionGroups()
78     {
79         if (fieldSystemPermissionGroups == null)
80         {
81             fieldSystemPermissionGroups = new ArrayList JavaDoc();
82         }
83         return fieldSystemPermissionGroups;
84     }
85
86     public void setSystemPermissionGroups(List JavaDoc inSystemPermissions)
87     {
88         fieldSystemPermissionGroups = inSystemPermissions;
89     }
90     
91     public List JavaDoc getSystemPermissions()
92     {
93         List JavaDoc permissions = new ArrayList JavaDoc();
94         for (Iterator JavaDoc iter = getSystemPermissionGroups().iterator(); iter.hasNext();)
95         {
96             PermissionGroup group = (PermissionGroup) iter.next();
97             permissions.addAll(group.getPermissions());
98         }
99         return permissions;
100     }
101
102     public PageManager getPageManager()
103     {
104         return fieldPageManager;
105     }
106
107     public void setPageManager(PageManager inPageManager)
108     {
109         fieldPageManager = inPageManager;
110     }
111
112     public void loadPermissions()
113     {
114         try
115         {
116             
117 // JarReader reader = new JarReader()
118
// {
119
// //Call back
120
// public void processFile( File inFile)
121
// {
122
// loadPermissionsDefs(inFile);
123
// }
124
// };
125
// reader.processInClasspath("permissions.xml");
126
// File lib = new File( getRootDir(), "WEB-INF/lib");
127
// reader.processInLibDir(lib, "permissions.xml");
128

129             try
130             {
131                 Enumeration JavaDoc pluginDefs = getClass().getClassLoader().getResources( "permissions.xml" );
132                 while( pluginDefs.hasMoreElements() )
133                 {
134                     URL JavaDoc url = (URL JavaDoc) pluginDefs.nextElement();
135                     addSystemPermissions( new InputStreamReader JavaDoc(url.openStream() ) );
136                 }
137             } catch ( IOException JavaDoc ex)
138             {
139                 throw new OpenEditRuntimeException(ex);
140             }
141
142         
143         }
144         catch ( Exception JavaDoc ex)
145         {
146             throw new OpenEditRuntimeException(ex);
147         }
148     }
149 // protected void loadPermissionsDefs(Reader inUrl)
150
// {
151
//
152
// FileReader reader;
153
// try
154
// {
155
// reader = new FileReader(inUrl);
156
// } catch (Exception ex)
157
// {
158
// throw new OpenEditRuntimeException(ex);
159
// }
160
// addSystemPermissions(reader);
161
// }
162

163     public File JavaDoc getRootDir()
164     {
165         return fieldRootDir;
166     }
167
168     public void setRootDir(File JavaDoc inRootDir)
169     {
170         fieldRootDir = inRootDir;
171     }
172
173 }
174
Popular Tags