001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.transfer;
022:
023: import java.util.List;
024: import com.methodhead.persistable.Persistable;
025: import com.methodhead.persistable.PersistableException;
026:
027: import org.apache.commons.beanutils.DynaClass;
028: import org.apache.commons.beanutils.DynaProperty;
029: import org.apache.commons.beanutils.BasicDynaClass;
030: import org.apache.commons.lang.StringUtils;
031: import org.apache.commons.lang.exception.ExceptionUtils;
032:
033: import com.methodhead.sitecontext.SiteContext;
034: import com.methodhead.property.Property;
035:
036: /**
037: * A SiteExtension. The following fields are defined:
038: * <ul>
039: * <li><tt>int sitecontext_id = 0</tt></li>
040: * <li><tt>String class_name = ""</tt></li>
041: * <li><tt>boolean enabled = true</tt></li>
042: * </ul>
043: */
044: public class SiteExtension extends Persistable {
045:
046: private static DynaClass dynaClass_ = null;
047:
048: static {
049: DynaProperty[] dynaProperties = new DynaProperty[] {
050: new DynaProperty("sitecontext_id", Integer.class),
051: new DynaProperty("class_name", String.class),
052: new DynaProperty("enabled", Boolean.class) };
053:
054: dynaClass_ = new BasicDynaClass("transfer_siteextension",
055: SiteExtension.class, dynaProperties);
056: }
057:
058: // constructors /////////////////////////////////////////////////////////////
059:
060: public SiteExtension() {
061: super (dynaClass_);
062: init();
063: }
064:
065: public SiteExtension(DynaClass dynaClass) {
066: super (dynaClass);
067: init();
068: }
069:
070: // constants ////////////////////////////////////////////////////////////////
071:
072: /**
073: * Property storing comma-separated list of extensions installed on the
074: * system.
075: */
076: public static final String PROPERTY_EXTENSIONS = "com.methodhead.transfer.Extensions";
077:
078: // classes //////////////////////////////////////////////////////////////////
079:
080: // methods //////////////////////////////////////////////////////////////////
081:
082: private void init() {
083: setInt("sitecontext_id", 0);
084: setString("class_name", "");
085: setBoolean("enabled", true);
086: }
087:
088: /**
089: * Loads the site extension for <tt>siteContext</tt> and <tt>className</tt>.
090: */
091: public void load(SiteContext siteContext, String className) {
092:
093: load("sitecontext_id=" + siteContext.getInt("id")
094: + " AND class_name=" + getSqlLiteral(className));
095: }
096:
097: /**
098: * Saves the site extension. It should already be loaded or have its
099: * <tt>sitecontext_id</tt> and <tt>class_name</tt> properties set.
100: */
101: public void save() {
102:
103: if ((getInt("sitecontext_id") == 0)
104: || StringUtils.isBlank(getString("class_name"))) {
105: throw new RuntimeException(
106: "sitecontext_id or class_name has not been set.");
107: }
108:
109: save("sitecontext_id=" + getInt("sitecontext_id")
110: + " AND class_name="
111: + getSqlLiteral(getString("class_name")));
112: }
113:
114: /**
115: * Deletes the site extension. It should already be loaded or have its
116: * <tt>sitecontext_id</tt> and <tt>class_name</tt> properties set.
117: */
118: public void delete() {
119:
120: if ((getInt("sitecontext_id") == 0)
121: || StringUtils.isBlank(getString("class_name"))) {
122: throw new RuntimeException(
123: "sitecontext_id or class_name has not been set.");
124: }
125:
126: deleteAll("sitecontext_id=" + getInt("sitecontext_id")
127: + " AND class_name="
128: + getSqlLiteral(getString("class_name")));
129: }
130:
131: /**
132: * Returns the extensions installed on the system. These extensions are
133: * defined by the comma-separated list of class names stored in the {@link
134: * #PROPERTY_EXTENSIONS} property of the default site context.
135: */
136: public Extension[] getInstalledExtensions() {
137:
138: //
139: // get property from default context
140: //
141: String extensionsStr = Property.getProperty(SiteContext
142: .getDefaultContext(), PROPERTY_EXTENSIONS);
143:
144: //
145: // blank or null?
146: //
147: if (StringUtils.isBlank(extensionsStr))
148: return new Extension[] {};
149:
150: //
151: // split string
152: //
153: String[] extensionStrs = extensionsStr.split(",");
154:
155: //
156: // instantiate extensions
157: //
158: Extension[] extensions = new Extension[extensionStrs.length];
159:
160: for (int i = 0; i < extensions.length; i++)
161: extensions[i] = instantiateExtension(StringUtils
162: .trim(extensionStrs[i]));
163:
164: return extensions;
165: }
166:
167: /**
168: * Loads all SiteExtensions for <tt>siteContext</tt> ordered by class_name.
169: */
170: public List loadAllForSiteContext(SiteContext siteContext) {
171:
172: return loadAll(dynaClass, "sitecontext_id="
173: + siteContext.getInt("id"), "class_name");
174: }
175:
176: /**
177: * Returns extensions for <tt>siteContext</tt>.
178: */
179: public Extension[] getExtensionsForSiteContext(
180: SiteContext siteContext) {
181:
182: List siteExtensions = loadAllForSiteContext(siteContext);
183:
184: //
185: // instantiate extensions
186: //
187: Extension[] extensions = new Extension[siteExtensions.size()];
188:
189: for (int i = 0; i < siteExtensions.size(); i++) {
190: SiteExtension siteExtension = (SiteExtension) siteExtensions
191: .get(i);
192:
193: Extension extension = instantiateExtension(siteExtension
194: .getString("class_name"));
195:
196: extensions[i] = extension;
197: }
198:
199: return extensions;
200: }
201:
202: /**
203: * Instantiates <tt>className</tt>.
204: */
205: public static Extension instantiateExtension(String className) {
206:
207: try {
208: return (Extension) Class.forName(className).newInstance();
209: } catch (ClassNotFoundException e) {
210: throw new RuntimeException(
211: "Unexpected ClassNotFoundException."
212: + ExceptionUtils.getStackTrace(e));
213: } catch (InstantiationException e) {
214: throw new RuntimeException(
215: "Unexpected InstantiationException trying to instantiate."
216: + ExceptionUtils.getStackTrace(e));
217: } catch (IllegalAccessException e) {
218: throw new RuntimeException(
219: "Unexpected IllegalAccessException trying to instantiate."
220: + ExceptionUtils.getStackTrace(e));
221: }
222: }
223:
224: /**
225: * Returns <code>true</code> if the extension provided by
226: * <code>className</code> exists and is enabled for <code>siteContext</code>.
227: */
228: public boolean isExtensionActive(SiteContext siteContext,
229: String className) {
230:
231: SiteExtension siteExtension = new SiteExtension();
232:
233: try {
234: siteExtension.load(siteContext, className);
235: } catch (PersistableException e) {
236: return false;
237: }
238:
239: return siteExtension.getBoolean("enabled");
240: }
241:
242: // properties ///////////////////////////////////////////////////////////////
243:
244: // attributes ///////////////////////////////////////////////////////////////
245: }
|