1 30 31 34 package com.nightlabs.ipanema.organisation; 35 36 import java.io.Serializable ; 37 import java.util.Collection ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 import java.util.StringTokenizer ; 42 43 import javax.jdo.PersistenceManager; 44 45 48 49 56 public class LocalOrganisation implements Serializable  57 { 58 78 protected Map passwords = new HashMap (); 79 80 95 protected Map pendingRegistrations = new HashMap (); 96 97 public static LocalOrganisation getLocalOrganisation(PersistenceManager pm) 98 { 99 Iterator it = pm.getExtent(LocalOrganisation.class).iterator(); 100 if (!it.hasNext()) 101 throw new IllegalStateException ("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 117 private String organisationID; 118 119 122 private Organisation organisation; 123 124 127 public Organisation getOrganisation() { 128 return organisation; 129 } 130 133 public String getOrganisationID() 134 { 135 return organisationID; 136 } 137 138 protected String encrypt(String organisationID, String pw) 139 { 140 if (pw == null) 141 return null; 142 143 StringBuffer sb = new StringBuffer (); 144 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 decrypt(String organisationID, String pw) 162 { 163 if (pw == null) 164 return null; 165 166 StringBuffer res = new StringBuffer (); 167 168 char prefix = pw.charAt(0); 169 pw = pw.substring(1); 170 171 if (prefix == 'i') { 172 StringTokenizer st = new StringTokenizer (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 ("Password is encrypted with an unknown encryption mechanism!"); 186 187 return res.toString(); 188 } 189 190 public void setPassword(String organisationID, String plainPassword) 191 { 192 if (organisationID == null) 193 throw new NullPointerException ("organisationID"); 194 195 if (plainPassword == null) 196 throw new NullPointerException ("plainPassword"); 197 198 passwords.put(organisationID, encrypt(organisationID, plainPassword)); 199 } 200 public String getPassword(String organisationID) 201 { 202 return decrypt(organisationID, (String ) passwords.get(organisationID)); 203 } 204 205 public void addPendingRegistration(RegistrationStatus registrationStatus) 206 { 207 String organisationID = registrationStatus.getOrganisationID(); 208 214 pendingRegistrations.put(organisationID, registrationStatus); 215 } 216 217 public Collection getPendingRegistrations() 218 { 219 return pendingRegistrations.values(); 220 } 221 222 public RegistrationStatus getPendingRegistration(String organisationID) 223 { 224 return (RegistrationStatus)pendingRegistrations.get(organisationID); 225 } 226 227 public void removePendingRegistration(String organisationID) 228 { 229 RegistrationStatus pendingRegistration = (RegistrationStatus)pendingRegistrations 230 .remove(organisationID); 231 } 232 } 233
| Popular Tags
|