1 16 17 18 package org.apache.catalina.cluster.session; 19 20 21 import java.util.Arrays ; 22 import java.util.List ; 23 import org.apache.catalina.Realm; 24 25 26 34 import org.apache.catalina.realm.GenericPrincipal; 35 public class SerializablePrincipal implements java.io.Serializable { 36 37 38 40 public SerializablePrincipal() 41 { 42 super(); 43 } 44 52 public SerializablePrincipal(Realm realm, String name, String password) { 53 54 this(realm, name, password, null); 55 56 } 57 58 59 69 public SerializablePrincipal(Realm realm, String name, String password, 70 List roles) { 71 72 super(); 73 this.realm = realm; 74 this.name = name; 75 this.password = password; 76 if (roles != null) { 77 this.roles = new String [roles.size()]; 78 this.roles = (String []) roles.toArray(this.roles); 79 if (this.roles.length > 0) 80 Arrays.sort(this.roles); 81 } 82 83 } 84 85 86 88 89 92 protected String name = null; 93 94 public String getName() { 95 return (this.name); 96 } 97 98 99 103 protected String password = null; 104 105 public String getPassword() { 106 return (this.password); 107 } 108 109 110 113 protected transient Realm realm = null; 114 115 public Realm getRealm() { 116 return (this.realm); 117 } 118 119 public void setRealm(Realm realm) { 120 this.realm = realm; 121 } 122 123 124 125 126 129 protected String roles[] = new String [0]; 130 131 public String [] getRoles() { 132 return (this.roles); 133 } 134 135 136 138 139 140 141 145 public String toString() { 146 147 StringBuffer sb = new StringBuffer ("SerializablePrincipal["); 148 sb.append(this.name); 149 sb.append("]"); 150 return (sb.toString()); 151 152 } 153 154 public static SerializablePrincipal createPrincipal(GenericPrincipal principal) 155 { 156 if ( principal==null) return null; 157 return new SerializablePrincipal(principal.getRealm(), 158 principal.getName(), 159 principal.getPassword(), 160 principal.getRoles()!=null?Arrays.asList(principal.getRoles()):null); 161 } 162 163 public GenericPrincipal getPrincipal( Realm realm ) 164 { 165 return new GenericPrincipal(realm,name,password,getRoles()!=null?Arrays.asList(getRoles()):null); 166 } 167 168 public static GenericPrincipal readPrincipal(java.io.ObjectInputStream in, Realm realm) throws java.io.IOException { 169 String name = in.readUTF(); 170 boolean hasPwd = in.readBoolean(); 171 String pwd = null; 172 if ( hasPwd ) pwd = in.readUTF(); 173 int size = in.readInt(); 174 String [] roles = new String [size]; 175 for ( int i=0; i<size; i++ ) roles[i] = in.readUTF(); 176 return new GenericPrincipal(realm,name,pwd,Arrays.asList(roles)); 177 } 178 179 public static void writePrincipal(GenericPrincipal p, java.io.ObjectOutputStream out) throws java.io.IOException { 180 out.writeUTF(p.getName()); 181 out.writeBoolean(p.getPassword()!=null); 182 if ( p.getPassword()!= null ) out.writeUTF(p.getPassword()); 183 String [] roles = p.getRoles(); 184 if ( roles == null ) roles = new String [0]; 185 out.writeInt(roles.length); 186 for ( int i=0; i<roles.length; i++ ) out.writeUTF(roles[i]); 187 } 188 189 190 } 191 | Popular Tags |