1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.astrogrid.community.install.loader ;
19
20 import java.net.URL ;
21 import java.net.URISyntaxException ;
22 import java.net.MalformedURLException ;
23
24 import java.io.IOException ;
25
26
27 import java.util.HashMap ;
28 import java.util.Iterator ;
29 import java.util.Collection ;
30 import java.util.Enumeration ;
31
32 import org.xml.sax.InputSource;
33
34 import org.exolab.castor.xml.Unmarshaller;
35 import org.exolab.castor.xml.CastorException ;
36
37 import org.exolab.castor.mapping.Mapping;
38 import org.exolab.castor.mapping.MappingException;
39
40 import org.astrogrid.store.Ivorn ;
41
42 import org.astrogrid.registry.RegistryException;
43
44 import org.astrogrid.community.common.ivorn.CommunityServiceIvornFactory ;
45
46 import org.astrogrid.community.common.policy.data.GroupData ;
47 import org.astrogrid.community.common.policy.data.AccountData ;
48
49 import org.astrogrid.community.common.exception.CommunityPolicyException ;
50 import org.astrogrid.community.common.exception.CommunityServiceException ;
51 import org.astrogrid.community.common.exception.CommunitySecurityException ;
52 import org.astrogrid.community.common.exception.CommunityIdentifierException ;
53
54 import org.astrogrid.community.client.policy.manager.PolicyManagerDelegate ;
55 import org.astrogrid.community.client.security.manager.SecurityManagerDelegate ;
56
57 /***
58 * A utility to load Community data from an XML file.
59 *
60 */
61 public class CommunityLoader
62 {
63 /***
64 * Switch for our debug statements.
65 * @todo Refactor to use the common logging.
66 *
67 */
68 private static boolean DEBUG_FLAG = true ;
69
70 /***
71 * The default name for our XML mapping file.
72 *
73 */
74 private static final String XML_MAPPING_NAME = "/org/astrogrid/community/install/loader/mapping.xml" ;
75
76 /***
77 * Public constructor.
78 * @param policyManager A reference to the PolicyManagerDelegate to upload the data to.
79 * @param securityManager A reference to the SecurityManagerDelegate to upload the data to.
80 *
81 */
82 public CommunityLoader(PolicyManagerDelegate policyManager, SecurityManagerDelegate securityManager)
83 {
84 this.policyManager = policyManager ;
85 this.securityManager = securityManager ;
86 }
87
88 /***
89 * Our PolicyManagerDelegate.
90 *
91 */
92 private PolicyManagerDelegate policyManager ;
93
94 /***
95 * Our SecurityManagerDelegate.
96 *
97 */
98 private SecurityManagerDelegate securityManager ;
99
100 /***
101 * Our Community data.
102 *
103 */
104 private CommunityLoaderData data ;
105
106 /***
107 * Load our Community data from our source.
108 *
109 */
110 public void load(URL source)
111 throws IOException, CommunityServiceException
112 {
113 if (DEBUG_FLAG) System.out.println("") ;
114 if (DEBUG_FLAG) System.out.println("----\"----") ;
115 if (DEBUG_FLAG) System.out.println("CommunityLoader.load()") ;
116
117
118 if (null == source)
119 {
120 throw new CommunityServiceException(
121 "Null Community data URL"
122 ) ;
123 }
124
125
126 try {
127
128
129 URL map = this.getClass().getResource(
130 XML_MAPPING_NAME
131 ) ;
132 if (null == map)
133 {
134 throw new CommunityServiceException(
135 "Unable to locate XML mapping"
136 ) ;
137 }
138
139
140 Mapping mapping = new Mapping();
141
142
143 mapping.loadMapping(map) ;
144 if (DEBUG_FLAG) System.out.println("PASS : Got mapping") ;
145
146
147 Unmarshaller marshaller = new Unmarshaller(mapping);
148 if (DEBUG_FLAG) System.out.println("PASS : Got marshaller") ;
149
150
151 this.data = (CommunityLoaderData) marshaller.unmarshal(
152 new InputSource(
153 source.openStream()
154 )
155 ) ;
156 if (DEBUG_FLAG) System.out.println("PASS : Got data") ;
157 }
158 catch (CastorException ouch)
159 {
160 if (DEBUG_FLAG) System.out.println("----\"----") ;
161 if (DEBUG_FLAG) System.out.println("Castor Exception") ;
162 if (DEBUG_FLAG) System.out.println(ouch) ;
163 throw new CommunityServiceException(
164 "Error occurred loading Community data",
165 ouch
166 ) ;
167 }
168 catch (MappingException ouch)
169 {
170 if (DEBUG_FLAG) System.out.println("----\"----") ;
171 if (DEBUG_FLAG) System.out.println("Mapping Exception") ;
172 if (DEBUG_FLAG) System.out.println(ouch) ;
173 throw new CommunityServiceException(
174 "Error occurred loading Community data",
175 ouch
176 ) ;
177 }
178 }
179
180 /***
181 * Upload our Community data.
182 *
183 */
184 public void upload()
185 throws
186 RegistryException,
187 CommunityServiceException,
188 CommunitySecurityException,
189 CommunityIdentifierException,
190 CommunityPolicyException
191 {
192 if (DEBUG_FLAG) System.out.println("") ;
193 if (DEBUG_FLAG) System.out.println("----\"----") ;
194 if (DEBUG_FLAG) System.out.println("CommunityLoader.upload()") ;
195
196
197 if (null == data)
198 {
199 throw new CommunityServiceException(
200 "No community data loaded"
201 ) ;
202 }
203
204
205 if (null == this.policyManager)
206 {
207 throw new CommunityServiceException(
208 "PolicyManager not configured"
209 ) ;
210 }
211
212
213 if (null == this.securityManager)
214 {
215 throw new CommunityServiceException(
216 "SecurityManager not configured"
217 ) ;
218 }
219
220
221 Iterator iter ;
222 iter = data.getAccounts().iterator() ;
223 while (iter.hasNext())
224 {
225 AccountData account = (AccountData) iter.next() ;
226 if (DEBUG_FLAG) System.out.println("----") ;
227 if (DEBUG_FLAG) System.out.println("Account : " + account.getIdent()) ;
228 this.policyManager.addAccount(
229 account
230 ) ;
231 }
232
233
234 iter = data.getGroups().iterator() ;
235 while (iter.hasNext())
236 {
237 GroupData group = (GroupData) iter.next() ;
238 if (DEBUG_FLAG) System.out.println("----") ;
239 if (DEBUG_FLAG) System.out.println("Group : " + group.getIdent()) ;
240 this.policyManager.addGroup(
241 group
242 ) ;
243 }
244
245
246 HashMap passwords = data.getPasswords() ;
247 iter = passwords.keySet().iterator() ;
248 while (iter.hasNext())
249 {
250 String account = (String) iter.next() ;
251 String password = (String) passwords.get(account) ;
252 if (DEBUG_FLAG) System.out.println("----") ;
253 if (DEBUG_FLAG) System.out.println("Account : " + account) ;
254 if (DEBUG_FLAG) System.out.println("Password : " + password) ;
255 this.securityManager.setPassword(
256 account,
257 password
258 ) ;
259 }
260 }
261 }
262