001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.administrator;
020:
021: import java.util.*;
022:
023: import org.lucane.common.*;
024: import org.lucane.common.concepts.*;
025: import org.lucane.common.net.ObjectConnection;
026: import org.lucane.server.*;
027: import org.lucane.server.store.*;
028:
029: public class AdministratorService extends Service {
030: private Store store;
031:
032: public AdministratorService() {
033: }
034:
035: public void init(Server parent) {
036: try {
037: this .store = parent.getStore();
038: } catch (Exception e) {
039: e.printStackTrace();
040: }
041: }
042:
043: public void process(ObjectConnection oc, Message message) {
044: AdminAction aa = (AdminAction) message.getData();
045: try {
046: switch (aa.action) {
047: case AdminAction.GET_ALL_GROUPS:
048: getAllGroups(oc);
049: break;
050: case AdminAction.GET_ALL_USERS:
051: getAllUsers(oc);
052: break;
053: case AdminAction.GET_ALL_PLUGINS:
054: getAllPlugins(oc);
055: break;
056: case AdminAction.GET_ALL_SERVICES:
057: getAllServices(oc);
058: break;
059: case AdminAction.GET_AUTHORIZED_PLUGINS:
060: getAuthorizedPlugins(oc, (UserConcept) aa.param);
061: break;
062:
063: case AdminAction.STORE:
064: store((Concept) aa.param);
065: oc.write("OK");
066: break;
067: case AdminAction.UPDATE:
068: update((Concept) aa.param);
069: oc.write("OK");
070: break;
071: case AdminAction.REMOVE:
072: remove((Concept) aa.param);
073: oc.write("OK");
074: break;
075:
076: case AdminAction.GET_USERS_FOR:
077: getUsersFor(oc, (Concept) aa.param);
078: break;
079: }
080: } catch (Exception e) {
081: try {
082: oc.write("FAILED " + e);
083: } catch (Exception e2) {
084: }
085:
086: e.printStackTrace();
087: }
088: }
089:
090: //-- get elements
091:
092: private void getAllGroups(ObjectConnection oc) {
093: ArrayList groups = new ArrayList();
094:
095: try {
096: Iterator i = store.getGroupStore().getAllGroups();
097: while (i.hasNext())
098: groups.add(i.next());
099: } catch (Exception e) {
100: e.printStackTrace();
101: }
102:
103: try {
104: oc.write("OK");
105: oc.write(groups);
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110:
111: private void getAllUsers(ObjectConnection oc) {
112: ArrayList users = new ArrayList();
113:
114: try {
115: Iterator i = store.getUserStore().getAllUsers();
116: while (i.hasNext())
117: users.add(i.next());
118: } catch (Exception e) {
119: e.printStackTrace();
120: }
121:
122: try {
123: oc.write("OK");
124: oc.write(users);
125: } catch (Exception e) {
126: e.printStackTrace();
127: }
128: }
129:
130: private void getAllPlugins(ObjectConnection oc) {
131: ArrayList plugins = new ArrayList();
132:
133: try {
134: Iterator i = store.getPluginStore().getAllPlugins();
135: while (i.hasNext())
136: plugins.add(i.next());
137: } catch (Exception e) {
138: e.printStackTrace();
139: }
140:
141: try {
142: oc.write("OK");
143: oc.write(plugins);
144: } catch (Exception e) {
145: e.printStackTrace();
146: }
147: }
148:
149: private void getAllServices(ObjectConnection oc) {
150: ArrayList services = new ArrayList();
151:
152: try {
153: Iterator i = store.getServiceStore().getAllServices();
154: while (i.hasNext())
155: services.add(i.next());
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159:
160: try {
161: oc.write("OK");
162: oc.write(services);
163: } catch (Exception e) {
164: e.printStackTrace();
165: }
166: }
167:
168: private void getAuthorizedPlugins(ObjectConnection oc,
169: UserConcept user) {
170: ArrayList plugins = new ArrayList();
171:
172: try {
173: Iterator i = store.getPluginStore().getAuthorizedPlugins(
174: user);
175: while (i.hasNext())
176: plugins.add(i.next());
177: } catch (Exception e) {
178: e.printStackTrace();
179: }
180:
181: try {
182: oc.write("OK");
183: oc.write(plugins);
184: } catch (Exception e) {
185: e.printStackTrace();
186: }
187: }
188:
189: //-- add, update, remove concepts
190:
191: private void store(Concept concept) throws Exception {
192: if (concept instanceof GroupConcept)
193: store.getGroupStore().storeGroup((GroupConcept) concept);
194: else if (concept instanceof UserConcept)
195: store.getUserStore().storeUser((UserConcept) concept);
196: else if (concept instanceof PluginConcept)
197: store.getPluginStore().storePlugin((PluginConcept) concept);
198: else if (concept instanceof ServiceConcept)
199: store.getServiceStore().storeService(
200: (ServiceConcept) concept);
201: }
202:
203: private void update(Concept concept) throws Exception {
204: if (concept instanceof GroupConcept)
205: store.getGroupStore().updateGroup((GroupConcept) concept);
206: else if (concept instanceof UserConcept)
207: store.getUserStore().updateUser((UserConcept) concept);
208: else if (concept instanceof PluginConcept)
209: store.getPluginStore()
210: .updatePlugin((PluginConcept) concept);
211: else if (concept instanceof ServiceConcept)
212: store.getServiceStore().updateService(
213: (ServiceConcept) concept);
214: }
215:
216: private void remove(Concept concept) throws Exception {
217: if (concept instanceof GroupConcept)
218: store.getGroupStore().removeGroup((GroupConcept) concept);
219: else if (concept instanceof UserConcept)
220: store.getUserStore().removeUser((UserConcept) concept);
221: else if (concept instanceof PluginConcept)
222: store.getPluginStore()
223: .removePlugin((PluginConcept) concept);
224: else if (concept instanceof ServiceConcept)
225: store.getServiceStore().removeService(
226: (ServiceConcept) concept);
227: }
228:
229: //-- get users for a particular concept
230:
231: private void getUsersFor(ObjectConnection oc, Concept concept)
232: throws Exception {
233: Iterator i;
234: ArrayList list = new ArrayList();
235:
236: if (concept instanceof GroupConcept)
237: i = store.getGroupStore().getUsersFor(
238: (GroupConcept) concept);
239: else if (concept instanceof PluginConcept)
240: i = store.getPluginStore().getUsersFor(
241: (PluginConcept) concept);
242: else if (concept instanceof ServiceConcept)
243: i = store.getServiceStore().getUsersFor(
244: (ServiceConcept) concept);
245: else
246: i = list.iterator();
247:
248: while (i.hasNext())
249: list.add(i.next());
250:
251: try {
252: oc.write("OK");
253: oc.write(list);
254: } catch (Exception e) {
255: e.printStackTrace();
256: }
257: }
258: }
|