KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > organisation > LocalOrganisation


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 14.06.2004
33  */

34 package com.nightlabs.ipanema.organisation;
35
36 import java.io.Serializable JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42
43 import javax.jdo.PersistenceManager;
44
45 /**
46  * @author marco
47  */

48
49 /**
50  * @jdo.persistence-capable
51  * identity-type = "datastore"
52  * detachable = "true"
53  *
54  * @jdo.inheritance strategy = "new-table"
55  */

56 public class LocalOrganisation implements Serializable JavaDoc
57 {
58     /**
59      * This Map stores the passwords of this organisation which
60      * are used to authenticate at other organisations.<br/><br/>
61      * Note, that the passwords in here are encrypted by an easily
62      * decryptable algorithm, whose only purpose is to avoid
63      * a simple SQL SELECT from disclose the plain text passwords to
64      * normal users.
65      * <br/><br/>
66      * key: String otherOrganisationID<br/>
67      * value: String password
68      *
69      * @jdo.field
70      * persistence-modifier="persistent"
71      * collection-type="map"
72      * key-type="java.lang.String"
73      * value-type="java.lang.String"
74      * dependent="true"
75      *
76      * @jdo.join
77      */

78     protected Map JavaDoc passwords = new HashMap JavaDoc();
79
80     /**
81      * This Map stores all inter-organisation-registrations which are currently
82      * pending between this LocalOrganisation and the contained other organisations.
83      * <br/><br/>
84      * key: String organisationID<br/>
85      * value: RegistrationStatus pendingRegistration
86      *
87      * @jdo.field
88      * persistence-modifier="persistent"
89      * collection-type="map"
90      * key-type="java.lang.String"
91      * value-type="RegistrationStatus"
92      *
93      * @jdo.join
94      */

95     protected Map JavaDoc pendingRegistrations = new HashMap JavaDoc();
96
97     public static LocalOrganisation getLocalOrganisation(PersistenceManager pm)
98     {
99         Iterator JavaDoc it = pm.getExtent(LocalOrganisation.class).iterator();
100         if (!it.hasNext())
101             throw new IllegalStateException JavaDoc("LocalOrganisation undefined in datastore!");
102         return (LocalOrganisation)it.next();
103     }
104
105     public LocalOrganisation() { }
106
107     public LocalOrganisation(Organisation _organisation)
108     {
109         this.organisation = _organisation;
110         this.organisationID = organisation.getOrganisationID();
111     }
112
113     /**
114      * @jdo.field persistence-modifier="persistent"
115      * @jdo.column length="100"
116      */

117     private String JavaDoc organisationID;
118
119     /**
120      * @jdo.field persistence-modifier="persistent"
121      */

122     private Organisation organisation;
123
124     /**
125      * @return Returns the organisation.
126      */

127     public Organisation getOrganisation() {
128         return organisation;
129     }
130     /**
131      * @return Returns the organisationID.
132      */

133     public String JavaDoc getOrganisationID()
134     {
135         return organisationID;
136     }
137
138     protected String JavaDoc encrypt(String JavaDoc organisationID, String JavaDoc pw)
139     {
140         if (pw == null)
141             return null;
142         
143         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144         // In case we'll change the encryption algorithm later,
145
// we use a prefix to mark the current one.
146
sb.append('i');
147         int oidx = 0;
148         for (int i = 0; i < pw.length(); ++i) {
149             int c = (int)pw.charAt(i);
150             int k = (int)organisationID.charAt(oidx++);
151             if (oidx >= organisationID.length())
152                 oidx = 0;
153             
154             int x = c ^ k;
155             sb.append(Integer.toHexString(x));
156             sb.append('.');
157         }
158         sb.deleteCharAt(sb.length() - 1);
159         return sb.toString();
160     }
161     protected String JavaDoc decrypt(String JavaDoc organisationID, String JavaDoc pw)
162     {
163         if (pw == null)
164             return null;
165         
166         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
167         
168         char prefix = pw.charAt(0);
169         pw = pw.substring(1);
170
171         if (prefix == 'i') {
172             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pw, ".", false);
173             int oidx = 0;
174             while (st.hasMoreTokens()) {
175                 int x = Integer.valueOf(st.nextToken(), 16).intValue();
176                 int k = (int)organisationID.charAt(oidx++);
177                 if (oidx >= organisationID.length())
178                     oidx = 0;
179                 
180                 int c = x ^ k;
181                 res.append((char)c);
182             }
183         }
184         else
185             throw new IllegalArgumentException JavaDoc("Password is encrypted with an unknown encryption mechanism!");
186
187         return res.toString();
188     }
189
190     public void setPassword(String JavaDoc organisationID, String JavaDoc plainPassword)
191     {
192         if (organisationID == null)
193             throw new NullPointerException JavaDoc("organisationID");
194
195         if (plainPassword == null)
196             throw new NullPointerException JavaDoc("plainPassword");
197
198         passwords.put(organisationID, encrypt(organisationID, plainPassword));
199     }
200     public String JavaDoc getPassword(String JavaDoc organisationID)
201     {
202         return decrypt(organisationID, (String JavaDoc) passwords.get(organisationID));
203     }
204     
205     public void addPendingRegistration(RegistrationStatus registrationStatus)
206     {
207         String JavaDoc organisationID = registrationStatus.getOrganisationID();
208         // It is probably better to ignore this. I think it's no problem
209
// if a registration is performed multiple times. Here, we always store
210
// the newest.
211
// if (pendingRegistrations.containsKey(organisationID))
212
// throw new IllegalStateException("There is already a registration pending for organisation \""+organisationID+"\"!");
213

214         pendingRegistrations.put(organisationID, registrationStatus);
215     }
216
217     public Collection JavaDoc getPendingRegistrations()
218     {
219         return pendingRegistrations.values();
220     }
221
222     public RegistrationStatus getPendingRegistration(String JavaDoc organisationID)
223     {
224         return (RegistrationStatus)pendingRegistrations.get(organisationID);
225     }
226     
227     public void removePendingRegistration(String JavaDoc organisationID)
228     {
229         RegistrationStatus pendingRegistration = (RegistrationStatus)pendingRegistrations
230                 .remove(organisationID);
231     }
232 }
233
Popular Tags