1 20 21 package com.methodhead.transfer; 22 23 import java.util.List ; 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 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 .class ), 54 new DynaProperty( "class_name", String .class ), 55 new DynaProperty( "enabled", Boolean .class ) 56 }; 57 58 dynaClass_ = 59 new BasicDynaClass( 60 "transfer_siteextension", SiteExtension.class, dynaProperties ); 61 } 62 63 65 public SiteExtension() { 66 super( dynaClass_ ); 67 init(); 68 } 69 70 public SiteExtension( 71 DynaClass dynaClass ) { 72 super( dynaClass ); 73 init(); 74 } 75 76 78 82 public static final String PROPERTY_EXTENSIONS = 83 "com.methodhead.transfer.Extensions"; 84 85 87 89 private void init() { 90 setInt( "sitecontext_id", 0 ); 91 setString( "class_name", "" ); 92 setBoolean( "enabled", true ); 93 } 94 95 98 public void load( 99 SiteContext siteContext, 100 String className ) { 101 102 load( 103 "sitecontext_id=" + siteContext.getInt( "id" ) + " AND class_name=" + 104 getSqlLiteral( className ) ); 105 } 106 107 111 public void save() { 112 113 if ( ( getInt( "sitecontext_id" ) == 0 ) || 114 StringUtils.isBlank( getString( "class_name" ) ) ) { 115 throw new RuntimeException ( 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 128 public void delete() { 129 130 if ( ( getInt( "sitecontext_id" ) == 0 ) || 131 StringUtils.isBlank( getString( "class_name" ) ) ) { 132 throw new RuntimeException ( 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 146 public Extension[] getInstalledExtensions() { 147 148 String extensionsStr = 152 Property.getProperty( 153 SiteContext.getDefaultContext(), PROPERTY_EXTENSIONS ); 154 155 if ( StringUtils.isBlank( extensionsStr ) ) 159 return new Extension[] {}; 160 161 String [] extensionStrs = extensionsStr.split( "," ); 165 166 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 181 public List loadAllForSiteContext( 182 SiteContext siteContext ) { 183 184 return loadAll( 185 dynaClass, "sitecontext_id=" + siteContext.getInt( "id" ), "class_name" ); 186 } 187 188 191 public Extension[] getExtensionsForSiteContext( 192 SiteContext siteContext ) { 193 194 List siteExtensions = loadAllForSiteContext( siteContext ); 195 196 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 216 public static Extension instantiateExtension( 217 String className ) { 218 219 try { 220 return ( Extension )Class.forName( className ).newInstance(); 221 } 222 catch ( ClassNotFoundException e ) { 223 throw new RuntimeException ( 224 "Unexpected ClassNotFoundException." + 225 ExceptionUtils.getStackTrace( e ) ); 226 } 227 catch ( InstantiationException e ) { 228 throw new RuntimeException ( 229 "Unexpected InstantiationException trying to instantiate." + 230 ExceptionUtils.getStackTrace( e ) ); 231 } 232 catch ( IllegalAccessException e ) { 233 throw new RuntimeException ( 234 "Unexpected IllegalAccessException trying to instantiate." + 235 ExceptionUtils.getStackTrace( e ) ); 236 } 237 } 238 239 243 public boolean isExtensionActive( 244 SiteContext siteContext, 245 String 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 261 } 263 | Popular Tags |