/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.photos.tapestry; import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.HashMap; import java.util.Properties; import org.apache.tapestry.ApplicationRuntimeException; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.Tapestry; import org.apache.tapestry.engine.BaseEngine; import org.apache.tapestry.engine.IPropertySource; import org.apache.tapestry.request.RequestContext; import org.wamblee.general.BeanKernel; import org.wamblee.io.ClassPathResource; import org.wamblee.photos.servlet.PhotoSession; import org.wamblee.security.authorization.AuthorizationService; import org.wamblee.usermgt.User; import org.wamblee.usermgt.UserAccessor; import org.wamblee.usermgt.UserAdministration; /** * Custom engine for the Photos application. This engine performs the following * tasks: * * */ public class PhotosEngine extends BaseEngine { /** * Name of the properties file for the photos application. */ private static final String PHOTOS_PROPERTIES = "org.wamblee.photos.properties"; /** * Name of the property describing the administrator group name. */ private static final String PROP_ADMIN_GROUP = "org.wamblee.photos.admingroup"; /** * Class name of the global class. */ private static final String PROP_GLOBAL_CLASS_NAME = "org.apache.tapestry.global-class"; /** * Message key used to describe the message when a Global object cannot be * constructed. */ private static final String KEY_GLOBAL_CONSTRUCTION_ERROR = "engine.unable-to-instantiate-global"; /** * Id of the password digester in the bean factory. */ private static final String PASSWORD_DIGESTER = "org.wamblee.usermgt.passwordDigester"; /** * Name of the administrator group. */ private String _adminGroup; /** * Constructs the engine. */ public PhotosEngine() throws IOException { super(); Properties props = new Properties(); InputStream propfile = new ClassPathResource(PHOTOS_PROPERTIES) .getInputStream(); try { props.load(propfile); _adminGroup = props.getProperty(PROP_ADMIN_GROUP); } finally { propfile.close(); } } /** * Creates the global object. It uses the property * {@link #PROP_GLOBAL_CLASS_NAME} to find the class name of the global * object. It then checks in turn for the following constructors of the * Global object: * */ protected Object createGlobal(RequestContext context) { String className = getPropertySource().getPropertyValue( PROP_GLOBAL_CLASS_NAME); if (className == null) return Collections.synchronizedMap(new HashMap()); Class globalClass = getResourceResolver().findClass(className); Object globalObject = null; try // Try to find the two-argument constructor { Object[] arguments = { context, getPropertySource() }; Class[] parameters = { RequestContext.class, IPropertySource.class }; globalObject = globalClass.getConstructor(parameters).newInstance( arguments); } catch (NoSuchMethodException e1) { try // Try to find the one-argument constructor { Object[] arguments = { getPropertySource() }; Class[] parameters = { IPropertySource.class }; globalObject = globalClass.getConstructor(parameters) .newInstance(arguments); } catch (NoSuchMethodException e2) { try // Try to find the zero-argument constructor { globalObject = globalClass.newInstance(); } catch (Exception e3) { throw new ApplicationRuntimeException(Tapestry.format( KEY_GLOBAL_CONSTRUCTION_ERROR, className), e3); } } catch (Exception e4) { throw new ApplicationRuntimeException(Tapestry.format( KEY_GLOBAL_CONSTRUCTION_ERROR, className), e4); } } catch (Exception e5) { throw new ApplicationRuntimeException(Tapestry.format( KEY_GLOBAL_CONSTRUCTION_ERROR, className), e5); } return globalObject; } /** * @return The photo album to use for this session. */ PhotoAlbum getPhotoAlbum(IRequestCycle aCycle) { return new PhotoAlbum(new PhotoSession(aCycle.getRequestContext() .getSession()).getAlbum()); } /** * Gets the group name of the administrator. * * @return Administrator group. */ String getAdministratorGroup() { return _adminGroup; } /** * Gets the currently logged in user. * * @return User or null if no user identity is known. */ User getUser() { return getUserAccessor().getCurrentUser(); } /** * @return The user accessor to use. */ UserAccessor getUserAccessor() { return BeanKernel.getBeanFactory().find(UserAccessor.class); } /** * @return The authorization service to use. */ AuthorizationService getAuthorizationService() { return BeanKernel.getBeanFactory().find(AuthorizationService.class); } /** * @return The user administration. */ UserAdministration getUserAdministration() { return BeanKernel.getBeanFactory().find(UserAdministration.class); } }