001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels.permissionsmanager;
007:
008: import java.sql.Connection;
009: import java.sql.ResultSet;
010: import java.sql.Statement;
011: import java.util.ArrayList;
012: import java.util.HashMap;
013:
014: import org.jasig.portal.IPermissible;
015: import org.jasig.portal.RDBMServices;
016: import org.jasig.portal.car.CarResources;
017: import org.jasig.portal.channels.error.SupportedPermissions;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: /**
022: * This class keeps track of IPermissible objects on behalf of CPermissionsManager,
023: * and is used to register new classes that generate CPermissionsManagerServants
024: *
025: * It also include hard coded references to 3 known IPermissibles, but will gracefully
026: * ignore them if they are not found
027: */
028: public class RDBMPermissibleRegistry {
029: private static final Log log = LogFactory
030: .getLog(RDBMPermissibleRegistry.class);
031: private static RDBMPermissibleRegistry _instance;
032: private HashMap owners = new HashMap();
033: private static final String findPermissibles = "SELECT IPERMISSIBLE_CLASS FROM UPC_PERM_MGR";
034:
035: /**
036: * put your documentation comment here
037: */
038: protected RDBMPermissibleRegistry() {
039: init();
040: }
041:
042: /**
043: * put your documentation comment here
044: */
045: private void init() {
046: log
047: .debug("PermissibleRegistryRDBM.init():: setting up registry");
048: Connection conn = null;
049: Statement st = null;
050: ResultSet rs = null;
051: try {
052: conn = getConnection();
053: st = conn.createStatement();
054: rs = st.executeQuery(findPermissibles);
055: while (rs.next()) {
056: String classname = rs.getString(1);
057: try {
058: Class newowner = CarResources.getInstance()
059: .getClassLoader().loadClass(classname);
060: owners.put(classname, newowner);
061: } catch (Exception e) {
062: if (log.isDebugEnabled())
063: log
064: .debug("PermissibleRegistryRDBM(): Could not instantiate IPermissible "
065: + e);
066: unregister(classname);
067: }
068: }
069: } catch (Exception e) {
070: log.error(e, e);
071: } finally {
072: RDBMServices.closeResultSet(rs);
073: RDBMServices.closeStatement(st);
074: releaseConnection(conn);
075: }
076: registerKnownPermissibles();
077: }
078:
079: /**
080: * put your documentation comment here
081: *
082: * @return a <code>RDBMPermissibleRegistry</code> singleton
083: */
084: public static synchronized RDBMPermissibleRegistry instance() {
085: if (_instance == null) {
086: _instance = new RDBMPermissibleRegistry();
087: }
088: return _instance;
089: }
090:
091: /**
092: * put your documentation comment here
093: * @return an array of <code>IPermissible</code>
094: */
095: public static IPermissible[] getAllPermissible() {
096: return instance().igetAllPermissible();
097: }
098:
099: /**
100: * put your documentation comment here
101: * @return an array of <code>IPermissible</code>
102: */
103: private IPermissible[] igetAllPermissible() {
104: //IPermissible[] ips = new IPermissible[owners.size()];
105: ArrayList prms = new ArrayList();
106: Class[] pclasses = (Class[]) owners.values().toArray(
107: new Class[owners.size()]);
108: for (int i = 0; i < owners.size(); i++) {
109: try {
110: IPermissible ip = (IPermissible) pclasses[i]
111: .newInstance();
112: prms.add(ip);
113: } catch (Exception e) {
114: if (log.isDebugEnabled())
115: log
116: .debug("RDBMPermissibleRegistry.igetAllPermissible(): Unable to instantiate IPermissible "
117: + e);
118: }
119: }
120: return (IPermissible[]) prms.toArray(new IPermissible[prms
121: .size()]);
122: }
123:
124: /*
125: * hard coded known included Permissibles
126: */
127: private void registerKnownPermissibles() {
128: iregisterPermissible("org.jasig.portal.channels.permissionsmanager.PermissiblePublishChannels");
129: iregisterPermissible("org.jasig.portal.channels.permissionsmanager.PermissibleSubscribeChannels");
130: iregisterPermissible("org.jasig.portal.channels.groupsmanager.CGroupsManager");
131: iregisterPermissible(SupportedPermissions.class.getName());
132: }
133:
134: /**
135: * put your documentation comment here
136: * @param classname
137: */
138: public static void registerPermissible(String classname) {
139: instance().iregisterPermissible(classname);
140: }
141:
142: /**
143: * put your documentation comment here
144: * @param classname
145: */
146: protected void iregisterPermissible(String classname) {
147: if (log.isDebugEnabled())
148: log
149: .debug("PermissibleRegistryRDBM.registerpermissible():: processing "
150: + classname);
151: if (!owners.containsKey(classname)) {
152: try {
153: IPermissible ip = (IPermissible) CarResources
154: .getInstance().getClassLoader().loadClass(
155: classname).newInstance();
156: if (ip != null) {
157: Connection conn = null;
158: Statement st = null;
159: try {
160: conn = getConnection();
161: st = conn.createStatement();
162: st
163: .executeUpdate("INSERT INTO UPC_PERM_MGR VALUES('"
164: + classname + "')");
165: owners.put(classname, CarResources
166: .getInstance().getClassLoader()
167: .loadClass(classname));
168: } catch (Exception e) {
169: log.error(e, e);
170: } finally {
171: RDBMServices.closeStatement(st);
172: releaseConnection(conn);
173: }
174: }
175: } catch (Throwable th) {
176: if (log.isDebugEnabled())
177: log
178: .debug("PermissibleRegistryRDBM.registerPermissible(): error while registering "
179: + classname + " : " + th);
180: }
181: }
182: }
183:
184: /**
185: * put your documentation comment here
186: * @param permissibleClass
187: */
188: private void unregister(String permissibleClass) {
189: Connection conn = null;
190: Statement st = null;
191: try {
192: conn = getConnection();
193: st = conn.createStatement();
194: st
195: .executeUpdate("DELETE FROM UPC_PERM_MGR WHERE IPERMISSIBLE_CLASS like '"
196: + permissibleClass + "'");
197: owners.remove(permissibleClass);
198: } catch (Exception e) {
199: log.debug(e, e);
200: } finally {
201: RDBMServices.closeStatement(st);
202: releaseConnection(conn);
203: }
204: }
205:
206: /**
207: * put your documentation comment here
208: * @return
209: */
210: protected Connection getConnection() {
211: return RDBMServices.getConnection();
212: }
213:
214: /**
215: * put your documentation comment here
216: * @param conn
217: */
218: protected void releaseConnection(Connection conn) {
219: RDBMServices.releaseConnection(conn);
220: }
221: }
|