KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > transfer > SiteExtension


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.transfer;
22
23 import java.util.List JavaDoc;
24 import com.methodhead.persistable.Persistable;
25 import com.methodhead.persistable.PersistableException;
26
27 import org.apache.commons.beanutils.DynaClass;
28 import org.apache.commons.beanutils.DynaProperty;
29 import org.apache.commons.beanutils.BasicDynaClass;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.commons.lang.exception.ExceptionUtils;
32
33 import com.methodhead.sitecontext.SiteContext;
34 import com.methodhead.property.Property;
35
36 /**
37  * A SiteExtension. The following fields are defined:
38  * <ul>
39  * <li><tt>int sitecontext_id = 0</tt></li>
40  * <li><tt>String class_name = ""</tt></li>
41  * <li><tt>boolean enabled = true</tt></li>
42  * </ul>
43  */

44 public class SiteExtension
45 extends
46   Persistable {
47
48   private static DynaClass dynaClass_ = null;
49
50   static {
51     DynaProperty[] dynaProperties =
52       new DynaProperty[] {
53         new DynaProperty( "sitecontext_id", Integer JavaDoc.class ),
54         new DynaProperty( "class_name", String JavaDoc.class ),
55         new DynaProperty( "enabled", Boolean JavaDoc.class )
56       };
57
58     dynaClass_ =
59       new BasicDynaClass(
60         "transfer_siteextension", SiteExtension.class, dynaProperties );
61   }
62
63   // constructors /////////////////////////////////////////////////////////////
64

65   public SiteExtension() {
66     super( dynaClass_ );
67     init();
68   }
69
70   public SiteExtension(
71     DynaClass dynaClass ) {
72     super( dynaClass );
73     init();
74   }
75
76   // constants ////////////////////////////////////////////////////////////////
77

78   /**
79    * Property storing comma-separated list of extensions installed on the
80    * system.
81    */

82   public static final String JavaDoc PROPERTY_EXTENSIONS =
83     "com.methodhead.transfer.Extensions";
84
85   // classes //////////////////////////////////////////////////////////////////
86

87   // methods //////////////////////////////////////////////////////////////////
88

89   private void init() {
90     setInt( "sitecontext_id", 0 );
91     setString( "class_name", "" );
92     setBoolean( "enabled", true );
93   }
94
95   /**
96    * Loads the site extension for <tt>siteContext</tt> and <tt>className</tt>.
97    */

98   public void load(
99     SiteContext siteContext,
100     String JavaDoc className ) {
101
102     load(
103       "sitecontext_id=" + siteContext.getInt( "id" ) + " AND class_name=" +
104       getSqlLiteral( className ) );
105   }
106
107   /**
108    * Saves the site extension. It should already be loaded or have its
109    * <tt>sitecontext_id</tt> and <tt>class_name</tt> properties set.
110    */

111   public void save() {
112
113     if ( ( getInt( "sitecontext_id" ) == 0 ) ||
114          StringUtils.isBlank( getString( "class_name" ) ) ) {
115       throw new RuntimeException JavaDoc(
116         "sitecontext_id or class_name has not been set." );
117     }
118
119     save(
120       "sitecontext_id=" + getInt( "sitecontext_id" ) + " AND class_name=" +
121       getSqlLiteral( getString( "class_name" ) ) );
122   }
123
124   /**
125    * Deletes the site extension. It should already be loaded or have its
126    * <tt>sitecontext_id</tt> and <tt>class_name</tt> properties set.
127    */

128   public void delete() {
129
130     if ( ( getInt( "sitecontext_id" ) == 0 ) ||
131          StringUtils.isBlank( getString( "class_name" ) ) ) {
132       throw new RuntimeException JavaDoc(
133         "sitecontext_id or class_name has not been set." );
134     }
135
136     deleteAll(
137       "sitecontext_id=" + getInt( "sitecontext_id" ) + " AND class_name=" +
138       getSqlLiteral( getString( "class_name" ) ) );
139   }
140
141   /**
142    * Returns the extensions installed on the system. These extensions are
143    * defined by the comma-separated list of class names stored in the {@link
144    * #PROPERTY_EXTENSIONS} property of the default site context.
145    */

146   public Extension[] getInstalledExtensions() {
147
148     //
149
// get property from default context
150
//
151
String JavaDoc extensionsStr =
152       Property.getProperty(
153         SiteContext.getDefaultContext(), PROPERTY_EXTENSIONS );
154
155     //
156
// blank or null?
157
//
158
if ( StringUtils.isBlank( extensionsStr ) )
159       return new Extension[] {};
160
161     //
162
// split string
163
//
164
String JavaDoc[] extensionStrs = extensionsStr.split( "," );
165
166     //
167
// instantiate extensions
168
//
169
Extension[] extensions = new Extension[ extensionStrs.length ];
170
171     for ( int i = 0; i < extensions.length; i++ )
172       extensions[ i ] =
173         instantiateExtension( StringUtils.trim( extensionStrs[ i ] ) );
174
175     return extensions;
176   }
177
178   /**
179    * Loads all SiteExtensions for <tt>siteContext</tt> ordered by class_name.
180    */

181   public List JavaDoc loadAllForSiteContext(
182     SiteContext siteContext ) {
183
184     return loadAll(
185       dynaClass, "sitecontext_id=" + siteContext.getInt( "id" ), "class_name" );
186   }
187
188   /**
189    * Returns extensions for <tt>siteContext</tt>.
190    */

191   public Extension[] getExtensionsForSiteContext(
192     SiteContext siteContext ) {
193
194     List JavaDoc siteExtensions = loadAllForSiteContext( siteContext );
195
196     //
197
// instantiate extensions
198
//
199
Extension[] extensions = new Extension[ siteExtensions.size() ];
200
201     for ( int i = 0; i < siteExtensions.size(); i++ ) {
202       SiteExtension siteExtension = ( SiteExtension )siteExtensions.get( i );
203
204       Extension extension =
205         instantiateExtension( siteExtension.getString( "class_name" ) );
206
207       extensions[ i ] = extension;
208     }
209
210     return extensions;
211   }
212
213   /**
214    * Instantiates <tt>className</tt>.
215    */

216   public static Extension instantiateExtension(
217     String JavaDoc className ) {
218
219     try {
220       return ( Extension )Class.forName( className ).newInstance();
221     }
222     catch ( ClassNotFoundException JavaDoc e ) {
223       throw new RuntimeException JavaDoc(
224         "Unexpected ClassNotFoundException." +
225         ExceptionUtils.getStackTrace( e ) );
226     }
227     catch ( InstantiationException JavaDoc e ) {
228       throw new RuntimeException JavaDoc(
229         "Unexpected InstantiationException trying to instantiate." +
230         ExceptionUtils.getStackTrace( e ) );
231     }
232     catch ( IllegalAccessException JavaDoc e ) {
233       throw new RuntimeException JavaDoc(
234         "Unexpected IllegalAccessException trying to instantiate." +
235         ExceptionUtils.getStackTrace( e ) );
236     }
237   }
238
239   /**
240    * Returns <code>true</code> if the extension provided by
241    * <code>className</code> exists and is enabled for <code>siteContext</code>.
242    */

243   public boolean isExtensionActive(
244     SiteContext siteContext,
245     String JavaDoc className ) {
246
247     SiteExtension siteExtension = new SiteExtension();
248
249     try {
250       siteExtension.load( siteContext, className );
251     }
252     catch( PersistableException e ) {
253       return false;
254     }
255
256     return siteExtension.getBoolean( "enabled" );
257   }
258
259   // properties ///////////////////////////////////////////////////////////////
260

261   // attributes ///////////////////////////////////////////////////////////////
262
}
263
Popular Tags