1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.impl;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.logging.Logger;
28 import java.util.prefs.BackingStoreException;
29 import java.util.prefs.Preferences;
30
31 import net.sourceforge.mflow.api.Account;
32 import net.sourceforge.mflow.api.AccountManager;
33 import net.sourceforge.mflow.reflect.ParamArgPair;
34 import net.sourceforge.mflow.reflect.ReflectionException;
35 import net.sourceforge.mflow.reflect.ReflectionUtil;
36
37 /***
38 * @author carrd
39 */
40 public class AccountManagerImpl implements AccountManager {
41 private static Logger log =
42 Logger.getLogger(AccountManagerImpl.class.getName());
43 private List accounts;
44 private Map types;
45
46 /***
47 * @todo make loadAccounts a seperate step for easier testing
48 */
49 AccountManagerImpl() {
50 this.types = new HashMap();
51 this.accounts = new ArrayList();
52 loadAccounts();
53 }
54
55 public Account createNewAccount(String name, Class typeClass) {
56 Preferences prefs =
57 Preferences.userNodeForPackage(getClass()).node("accounts");
58 try {
59 String[] children = prefs.childrenNames();
60 for (int i = 0; i < children.length; i++) {
61 if (children[i].equalsIgnoreCase(name)) {
62 return null; //only one account per name allowed
63 }
64 }
65 Preferences node = prefs.node(name);
66 node.put("class", typeClass.getName());
67 return instantiate(node);
68 } catch (BackingStoreException bse) {
69 log.throwing("PluginManager", "loadPlugins()", bse);
70 return null;
71 }
72 }
73
74 public List getAccounts() {
75 return this.accounts;
76 }
77
78 /***
79 * Returns an unmodifiable copy of the type {@link Map}. The keys are the
80 * names of the account types, while the keys are the type classes.
81 *
82 * @return an unmodifiable {@link Map} of {@link String}s to {@link Class}es
83 */
84 public Map getTypes() {
85 return Collections.unmodifiableMap(this.types);
86 }
87
88 private Account instantiate(Preferences node) {
89 Account ret = lookupAccount(node.name());
90 if (ret != null) {
91 return ret;
92 }
93 String className = node.get("class", null);
94 List paramArgs = new ArrayList();
95 Class[] argType = { Account.class };
96 Object[] args = { node };
97 paramArgs.add(new ParamArgPair(argType, args));
98 paramArgs.add(new ParamArgPair());
99 try {
100 ret = (Account) ReflectionUtil.instantiate(className, paramArgs);
101 this.accounts.add(ret);
102 return ret;
103 } catch (ReflectionException re) {
104 log.throwing(getClass().getName(), "instantiate", re);
105 } catch (ClassCastException cce) {
106 log.throwing(getClass().getName(), "instantiate", cce);
107 }
108 return ret;
109 }
110
111 private void loadAccounts() {
112 Preferences prefs =
113 Preferences.userNodeForPackage(getClass()).node("accounts");
114 try {
115 String[] children = prefs.childrenNames();
116 for (int i = 0; i < children.length; i++) {
117 String nodeName = children[i];
118 Preferences node = prefs.node(nodeName);
119 instantiate(node);
120 }
121 } catch (BackingStoreException bse) {
122 log.throwing("PluginManager", "loadPlugins()", bse);
123 }
124 }
125
126 private Account lookupAccount(String name) {
127 for (Iterator it = this.accounts.iterator(); it.hasNext();) {
128 Account account = (Account) it.next();
129 if (account.getName().equals(name)) {
130 return account;
131 }
132 }
133 return null;
134 }
135
136 public void registerType(String name, Class clazz) {
137 this.types.put(name, clazz);
138 }
139
140 public void remove(Account account) {
141 Preferences prefs =
142 Preferences.userNodeForPackage(getClass()).node("accounts");
143 try {
144 prefs.node(account.getName()).removeNode();
145 } catch (BackingStoreException bse) {
146 log.throwing("PluginManager", "loadPlugins()", bse);
147 }
148 }
149 }
This page was automatically generated by Maven