1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: public abstract class BaseKeyring
52: implements IKeyring
53: {
54:
55: protected PasswordAuthenticatedEntry keyring;
56: protected CompressedEntry keyring2;
57:
58: public BaseKeyring()
59: {
60: }
61:
62: public void load(Map attributes) throws IOException
63: {
64: InputStream in = (InputStream) attributes.get(KEYRING_DATA_IN);
65: if (in == null)
66: throw new IllegalArgumentException("no input stream");
67: char[] password = (char[]) attributes.get(KEYRING_PASSWORD);
68: if (password == null)
69: password = new char[0];
70:
71: if (in.read() != Registry.GKR_MAGIC[0]
72: || in.read() != Registry.GKR_MAGIC[1]
73: || in.read() != Registry.GKR_MAGIC[2]
74: || in.read() != Registry.GKR_MAGIC[3])
75: throw new MalformedKeyringException("magic");
76:
77: load(in, password);
78: List l = keyring.getEntries();
79: if (l.size() == 1 && (l.get(0) instanceof CompressedEntry))
80: keyring2 = (CompressedEntry) l.get(0);
81: }
82:
83: public void store(Map attributes) throws IOException
84: {
85: OutputStream out = (OutputStream) attributes.get(KEYRING_DATA_OUT);
86: if (out == null)
87: throw new IllegalArgumentException("no output stream");
88: char[] password = (char[]) attributes.get(KEYRING_PASSWORD);
89: if (password == null)
90: password = new char[0];
91: if (keyring == null)
92: throw new IllegalStateException("empty keyring");
93:
94: out.write(Registry.GKR_MAGIC);
95: store(out, password);
96: }
97:
98: public void reset()
99: {
100: keyring = null;
101: }
102:
103: public int size()
104: {
105: if (keyring == null)
106: throw new IllegalStateException("keyring not loaded");
107: return ((StringTokenizer) aliases()).countTokens();
108: }
109:
110: public Enumeration aliases()
111: {
112: if (keyring == null)
113: throw new IllegalStateException("keyring not loaded");
114: return new StringTokenizer(keyring.getAliasList(), ";");
115: }
116:
117: public boolean containsAlias(String alias)
118: {
119: if (keyring == null)
120: throw new IllegalStateException("keyring not loaded");
121: return keyring.containsAlias(alias);
122: }
123:
124: public List get(String alias)
125: {
126: if (keyring == null)
127: throw new IllegalStateException("keyring not loaded");
128: return keyring.get(alias);
129: }
130:
131: public void add(Entry entry)
132: {
133: if (keyring == null)
134: throw new IllegalStateException("keyring not loaded");
135: if (keyring2 != null)
136: keyring2.add(entry);
137: else
138: keyring.add(entry);
139: }
140:
141: public void remove(String alias)
142: {
143: if (keyring == null)
144: throw new IllegalStateException("keyring not loaded");
145: keyring.remove(alias);
146: }
147:
148: protected String fixAlias(String alias)
149: {
150: return alias.replace(';', '_');
151: }
152:
153: protected abstract void load(InputStream in, char[] password)
154: throws IOException;
155:
156: protected abstract void store(OutputStream out, char[] password)
157: throws IOException;
158: }