http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneEntityRestlet.java ---------------------------------------------------------------------- diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneEntityRestlet.java b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneEntityRestlet.java new file mode 100644 index 0000000..b76bfd3 --- /dev/null +++ b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneEntityRestlet.java @@ -0,0 +1,311 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.restlet; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import org.apache.polygene.api.common.Optional; +import org.apache.polygene.api.entity.EntityComposite; +import org.apache.polygene.api.identity.HasIdentity; +import org.apache.polygene.api.injection.scope.Service; +import org.apache.polygene.api.injection.scope.Structure; +import org.apache.polygene.api.injection.scope.Uses; +import org.apache.polygene.api.structure.Module; +import org.apache.polygene.api.unitofwork.NoSuchEntityException; +import org.apache.polygene.api.unitofwork.UnitOfWork; +import org.apache.polygene.api.unitofwork.UnitOfWorkFactory; +import org.apache.polygene.api.usecase.UsecaseBuilder; +import org.apache.polygene.api.value.ValueBuilder; +import org.apache.polygene.api.value.ValueBuilderFactory; +import org.apache.polygene.library.restlet.metainfo.UserIdentity; +import org.apache.polygene.library.restlet.repository.RepositoryLocator; +import org.apache.polygene.library.restlet.resource.DefaultResourceFactoryImpl; +import org.apache.polygene.library.restlet.resource.NotPresentException; +import org.apache.polygene.library.restlet.resource.ResourceFactory; +import org.apache.polygene.library.restlet.resource.ServerResource; +import org.apache.polygene.library.restlet.serialization.PolygeneConverter; +import org.apache.polygene.spi.PolygeneSPI; +import org.restlet.Context; +import org.restlet.Request; +import org.restlet.Response; +import org.restlet.Restlet; +import org.restlet.data.Form; +import org.restlet.data.Method; +import org.restlet.data.Parameter; +import org.restlet.data.Status; +import org.restlet.representation.Representation; +import org.restlet.representation.Variant; +import org.restlet.routing.Router; +import org.restlet.security.User; + +public class PolygeneEntityRestlet<T extends HasIdentity> extends Restlet +{ + /** + * Creates a new PolygeneEntityRestlet instance for the given resource and entity classes. + * <p> + * This utility method should be used in your org.restlet.Application to create routes. + * + * @param <K> Parameterized type of the resource + * @param <T> Parameterized type of the entity + * @param module Module to use for object instanciation + * @param router Restlet Router + * @param resourceClass Resource class + * @param entityClass Entity class + * + * @return The PolygeneEntityRestlet instance + */ + public static <K extends HasIdentity, T extends ServerResource<K>> Restlet newInstance( + Module module, Router router, Class<T> resourceClass, Class<K> entityClass + ) + { + @SuppressWarnings( "unchecked" ) + ResourceFactory<K, T> factory = module.newObject( DefaultResourceFactoryImpl.class, resourceClass, router ); + return module.newObject( PolygeneEntityRestlet.class, factory, router, entityClass, new PolygeneConverter( module ) ); + } + + @Structure + private ValueBuilderFactory vbf; + + @Structure + private UnitOfWorkFactory uowf; + + @Uses + private ResourceFactory resourceFactory; + + @Uses + private Router router; + + @Uses + @Optional + private Class<T> identityType; + + @Structure + private PolygeneSPI spi; + + @Uses + private PolygeneConverter converter; + + @Service + private RepositoryLocator locator; + + @Override + public void handle( Request request, Response response ) + { + try + { + super.handle( request, response ); + Method method = request.getMethod(); + if( method.equals( Method.GET ) ) + { + get( request, response ); + } + if( method.equals( Method.DELETE ) ) + { + delete( request, response ); + } + if( method.equals( Method.POST ) ) + { + post( request, response ); + } + if( method.equals( Method.PUT ) ) + { + put( request, response ); + } + } + catch( RuntimeException e ) + { + e.printStackTrace(); + throw e; + } + } + + private void get( Request request, Response response ) + { + execute( request, response, resource -> { + try + { + T result = resource.get(); + if( result != null ) + { + if( result instanceof EntityComposite ) + { + result = locator.find( identityType ).toValue( result ); + } + Representation representation = converter.toRepresentation( result, new Variant(), null ); + response.setEntity( representation ); + response.setEntity( representation ); + response.setStatus( Status.SUCCESS_OK ); + } + else + { + response.setStatus( Status.CLIENT_ERROR_NOT_FOUND ); + } + } + catch( NoSuchEntityException e ) + { + response.setStatus( Status.CLIENT_ERROR_NOT_FOUND, e, "Entity not found." ); + } + } + ); + } + + private void put( Request request, Response response ) + { + execute( request, response, resource -> { + + T value = convertToObject( identityType, request ); + resource.put( value ); + response.setStatus( Status.SUCCESS_OK ); + } ); + } + + private void delete( Request request, Response response ) + { + execute( request, response, resource -> { + resource.delete(); + response.setStatus( Status.SUCCESS_NO_CONTENT ); + } ); + } + + private void post( final Request request, final Response response ) + { + execute( request, response, resource -> { + RestForm form = createRestForm( request ); + RestLink link = resource.post( form ); + response.setLocationRef( link.path().get() ); + response.setStatus( Status.REDIRECTION_SEE_OTHER ); + } ); + } + + private RestForm createRestForm( final Request request ) + { + //noinspection MismatchedQueryAndUpdateOfCollection + Form form = new Form( request.getEntity() ); + ValueBuilder<RestForm> builder = vbf.newValueBuilderWithState( + RestForm.class, + descriptor -> { + if( descriptor.qualifiedName().name().equals( "fields" ) ) + { + List<FormField> result = new ArrayList<>(); + for( Parameter param : form ) + { + String name = param.getName(); + String value = param.getValue(); + ValueBuilder<FormField> fieldBuilder = vbf.newValueBuilder( FormField.class ); + FormField prototype = fieldBuilder.prototype(); + prototype.name().set( name ); + prototype.value().set( value ); + prototype.type().set( FormField.TEXT ); + result.add( fieldBuilder.newInstance() ); + } + return result; + } + return null; + }, + descriptor -> null, + descriptor -> null, + descriptor -> null + ); + return builder.newInstance(); + } + + private void execute( Request request, Response response, Consumer<ServerResource<T>> closure ) + { + UnitOfWork uow = null; + try + { + uow = createUnitOfWork( request ); + ServerResource<T> resource = createResource( request, response, getContext() ); + closure.accept( resource ); + uow.complete(); + } + catch( UnsupportedOperationException e ) + { + e.printStackTrace(); + response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST, e, e.getMessage() ); + } + catch( ConversionException e ) + { + e.printStackTrace(); + response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage() ); + } + catch( NotPresentException e ) + { + e.printStackTrace(); + response.setStatus( Status.CLIENT_ERROR_NOT_FOUND, e.getMessage() ); + } + catch( Throwable e ) + { + e.printStackTrace(); + response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage() ); + } + finally + { + if( uow != null && uow.isOpen() ) + { + uow.discard(); + } + } + } + + private ServerResource<T> createResource( Request request, Response response, Context context ) + { + @SuppressWarnings( "unchecked" ) + ServerResource<T> serverResource = resourceFactory.create( identityType, request, response, context ); + return serverResource; + } + + private UnitOfWork createUnitOfWork( Request request ) + { + UsecaseBuilder usecaseBuilder = UsecaseBuilder.buildUsecase( request.getResourceRef().getIdentifier( true ) ); + User user = request.getClientInfo().getUser(); + if( user != null ) + { + UserIdentity userIdentity = new UserIdentity( user.getIdentifier(), + user.getName(), + user.getEmail(), + user.getFirstName(), + user.getLastName() + ); + usecaseBuilder.withMetaInfo( userIdentity ); + } + return uowf.newUnitOfWork( usecaseBuilder.newUsecase() ); + } + + private <K> K convertToObject( Class<K> type, Request request ) + { + try + { + return converter.toObject( request.getEntity(), type, null ); + } + catch( IOException e ) + { + throw new ConversionException( request.getEntityAsText() ); + } + } + + @Override + public String toString() + { + return "PolygeneRestlet[" + ( identityType == null ? "<null>" : identityType.getSimpleName() ) + ", " + resourceFactory + "]"; + } +}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneServerServlet.java ---------------------------------------------------------------------- diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneServerServlet.java b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneServerServlet.java new file mode 100644 index 0000000..f925cbe --- /dev/null +++ b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/PolygeneServerServlet.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.restlet; + +import javax.servlet.Servlet; + +import org.apache.polygene.api.injection.scope.Structure; +import org.apache.polygene.api.mixin.Mixins; +import org.apache.polygene.api.structure.Module; +import org.restlet.Context; +import org.restlet.ext.servlet.ServerServlet; + +/** + * Restlet ServerServlet backed by a org.restlet.Application object. + */ +@Mixins( PolygeneServerServlet.Mixin.class ) +public interface PolygeneServerServlet + extends Servlet +{ + class Mixin + extends ServerServlet + { + private static final long serialVersionUID = 1L; + + @Structure + private Module module; + + @Override + protected org.restlet.Application createApplication( Context parentContext ) + { + return module.newObject( org.restlet.Application.class, parentContext.createChildContext() ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestEntityRestlet.java ---------------------------------------------------------------------- diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestEntityRestlet.java b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestEntityRestlet.java deleted file mode 100644 index b76bfd3..0000000 --- a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestEntityRestlet.java +++ /dev/null @@ -1,311 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.restlet; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; -import org.apache.polygene.api.common.Optional; -import org.apache.polygene.api.entity.EntityComposite; -import org.apache.polygene.api.identity.HasIdentity; -import org.apache.polygene.api.injection.scope.Service; -import org.apache.polygene.api.injection.scope.Structure; -import org.apache.polygene.api.injection.scope.Uses; -import org.apache.polygene.api.structure.Module; -import org.apache.polygene.api.unitofwork.NoSuchEntityException; -import org.apache.polygene.api.unitofwork.UnitOfWork; -import org.apache.polygene.api.unitofwork.UnitOfWorkFactory; -import org.apache.polygene.api.usecase.UsecaseBuilder; -import org.apache.polygene.api.value.ValueBuilder; -import org.apache.polygene.api.value.ValueBuilderFactory; -import org.apache.polygene.library.restlet.metainfo.UserIdentity; -import org.apache.polygene.library.restlet.repository.RepositoryLocator; -import org.apache.polygene.library.restlet.resource.DefaultResourceFactoryImpl; -import org.apache.polygene.library.restlet.resource.NotPresentException; -import org.apache.polygene.library.restlet.resource.ResourceFactory; -import org.apache.polygene.library.restlet.resource.ServerResource; -import org.apache.polygene.library.restlet.serialization.PolygeneConverter; -import org.apache.polygene.spi.PolygeneSPI; -import org.restlet.Context; -import org.restlet.Request; -import org.restlet.Response; -import org.restlet.Restlet; -import org.restlet.data.Form; -import org.restlet.data.Method; -import org.restlet.data.Parameter; -import org.restlet.data.Status; -import org.restlet.representation.Representation; -import org.restlet.representation.Variant; -import org.restlet.routing.Router; -import org.restlet.security.User; - -public class PolygeneEntityRestlet<T extends HasIdentity> extends Restlet -{ - /** - * Creates a new PolygeneEntityRestlet instance for the given resource and entity classes. - * <p> - * This utility method should be used in your org.restlet.Application to create routes. - * - * @param <K> Parameterized type of the resource - * @param <T> Parameterized type of the entity - * @param module Module to use for object instanciation - * @param router Restlet Router - * @param resourceClass Resource class - * @param entityClass Entity class - * - * @return The PolygeneEntityRestlet instance - */ - public static <K extends HasIdentity, T extends ServerResource<K>> Restlet newInstance( - Module module, Router router, Class<T> resourceClass, Class<K> entityClass - ) - { - @SuppressWarnings( "unchecked" ) - ResourceFactory<K, T> factory = module.newObject( DefaultResourceFactoryImpl.class, resourceClass, router ); - return module.newObject( PolygeneEntityRestlet.class, factory, router, entityClass, new PolygeneConverter( module ) ); - } - - @Structure - private ValueBuilderFactory vbf; - - @Structure - private UnitOfWorkFactory uowf; - - @Uses - private ResourceFactory resourceFactory; - - @Uses - private Router router; - - @Uses - @Optional - private Class<T> identityType; - - @Structure - private PolygeneSPI spi; - - @Uses - private PolygeneConverter converter; - - @Service - private RepositoryLocator locator; - - @Override - public void handle( Request request, Response response ) - { - try - { - super.handle( request, response ); - Method method = request.getMethod(); - if( method.equals( Method.GET ) ) - { - get( request, response ); - } - if( method.equals( Method.DELETE ) ) - { - delete( request, response ); - } - if( method.equals( Method.POST ) ) - { - post( request, response ); - } - if( method.equals( Method.PUT ) ) - { - put( request, response ); - } - } - catch( RuntimeException e ) - { - e.printStackTrace(); - throw e; - } - } - - private void get( Request request, Response response ) - { - execute( request, response, resource -> { - try - { - T result = resource.get(); - if( result != null ) - { - if( result instanceof EntityComposite ) - { - result = locator.find( identityType ).toValue( result ); - } - Representation representation = converter.toRepresentation( result, new Variant(), null ); - response.setEntity( representation ); - response.setEntity( representation ); - response.setStatus( Status.SUCCESS_OK ); - } - else - { - response.setStatus( Status.CLIENT_ERROR_NOT_FOUND ); - } - } - catch( NoSuchEntityException e ) - { - response.setStatus( Status.CLIENT_ERROR_NOT_FOUND, e, "Entity not found." ); - } - } - ); - } - - private void put( Request request, Response response ) - { - execute( request, response, resource -> { - - T value = convertToObject( identityType, request ); - resource.put( value ); - response.setStatus( Status.SUCCESS_OK ); - } ); - } - - private void delete( Request request, Response response ) - { - execute( request, response, resource -> { - resource.delete(); - response.setStatus( Status.SUCCESS_NO_CONTENT ); - } ); - } - - private void post( final Request request, final Response response ) - { - execute( request, response, resource -> { - RestForm form = createRestForm( request ); - RestLink link = resource.post( form ); - response.setLocationRef( link.path().get() ); - response.setStatus( Status.REDIRECTION_SEE_OTHER ); - } ); - } - - private RestForm createRestForm( final Request request ) - { - //noinspection MismatchedQueryAndUpdateOfCollection - Form form = new Form( request.getEntity() ); - ValueBuilder<RestForm> builder = vbf.newValueBuilderWithState( - RestForm.class, - descriptor -> { - if( descriptor.qualifiedName().name().equals( "fields" ) ) - { - List<FormField> result = new ArrayList<>(); - for( Parameter param : form ) - { - String name = param.getName(); - String value = param.getValue(); - ValueBuilder<FormField> fieldBuilder = vbf.newValueBuilder( FormField.class ); - FormField prototype = fieldBuilder.prototype(); - prototype.name().set( name ); - prototype.value().set( value ); - prototype.type().set( FormField.TEXT ); - result.add( fieldBuilder.newInstance() ); - } - return result; - } - return null; - }, - descriptor -> null, - descriptor -> null, - descriptor -> null - ); - return builder.newInstance(); - } - - private void execute( Request request, Response response, Consumer<ServerResource<T>> closure ) - { - UnitOfWork uow = null; - try - { - uow = createUnitOfWork( request ); - ServerResource<T> resource = createResource( request, response, getContext() ); - closure.accept( resource ); - uow.complete(); - } - catch( UnsupportedOperationException e ) - { - e.printStackTrace(); - response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST, e, e.getMessage() ); - } - catch( ConversionException e ) - { - e.printStackTrace(); - response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage() ); - } - catch( NotPresentException e ) - { - e.printStackTrace(); - response.setStatus( Status.CLIENT_ERROR_NOT_FOUND, e.getMessage() ); - } - catch( Throwable e ) - { - e.printStackTrace(); - response.setStatus( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage() ); - } - finally - { - if( uow != null && uow.isOpen() ) - { - uow.discard(); - } - } - } - - private ServerResource<T> createResource( Request request, Response response, Context context ) - { - @SuppressWarnings( "unchecked" ) - ServerResource<T> serverResource = resourceFactory.create( identityType, request, response, context ); - return serverResource; - } - - private UnitOfWork createUnitOfWork( Request request ) - { - UsecaseBuilder usecaseBuilder = UsecaseBuilder.buildUsecase( request.getResourceRef().getIdentifier( true ) ); - User user = request.getClientInfo().getUser(); - if( user != null ) - { - UserIdentity userIdentity = new UserIdentity( user.getIdentifier(), - user.getName(), - user.getEmail(), - user.getFirstName(), - user.getLastName() - ); - usecaseBuilder.withMetaInfo( userIdentity ); - } - return uowf.newUnitOfWork( usecaseBuilder.newUsecase() ); - } - - private <K> K convertToObject( Class<K> type, Request request ) - { - try - { - return converter.toObject( request.getEntity(), type, null ); - } - catch( IOException e ) - { - throw new ConversionException( request.getEntityAsText() ); - } - } - - @Override - public String toString() - { - return "PolygeneRestlet[" + ( identityType == null ? "<null>" : identityType.getSimpleName() ) + ", " + resourceFactory + "]"; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestServerServlet.java ---------------------------------------------------------------------- diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestServerServlet.java b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestServerServlet.java deleted file mode 100644 index f925cbe..0000000 --- a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/ZestServerServlet.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.restlet; - -import javax.servlet.Servlet; - -import org.apache.polygene.api.injection.scope.Structure; -import org.apache.polygene.api.mixin.Mixins; -import org.apache.polygene.api.structure.Module; -import org.restlet.Context; -import org.restlet.ext.servlet.ServerServlet; - -/** - * Restlet ServerServlet backed by a org.restlet.Application object. - */ -@Mixins( PolygeneServerServlet.Mixin.class ) -public interface PolygeneServerServlet - extends Servlet -{ - class Mixin - extends ServerServlet - { - private static final long serialVersionUID = 1L; - - @Structure - private Module module; - - @Override - protected org.restlet.Application createApplication( Context parentContext ) - { - return module.newObject( org.restlet.Application.class, parentContext.createChildContext() ); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/PolygeneConverter.java ---------------------------------------------------------------------- diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/PolygeneConverter.java b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/PolygeneConverter.java new file mode 100644 index 0000000..17739b6 --- /dev/null +++ b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/PolygeneConverter.java @@ -0,0 +1,268 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.restlet.serialization; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.polygene.api.injection.scope.Structure; +import org.apache.polygene.api.object.ObjectFactory; +import org.apache.polygene.spi.PolygeneSPI; +import org.restlet.data.MediaType; +import org.restlet.data.Preference; +import org.restlet.engine.converter.ConverterHelper; +import org.restlet.engine.resource.VariantInfo; +import org.restlet.representation.Representation; +import org.restlet.representation.Variant; +import org.restlet.resource.Resource; + +/** + * Converter between Apache Polygene and JSON. + */ +public class PolygeneConverter extends ConverterHelper +{ + @Structure + private PolygeneSPI spi; + + /** + * Variant with media type application/json. + */ + private static final VariantInfo VARIANT_JSON = new VariantInfo( MediaType.APPLICATION_JSON ); + private static final VariantInfo VARIANT_WWW_FORM_URLENCODED = new VariantInfo( MediaType.APPLICATION_WWW_FORM ); + + private final ObjectFactory objectFactory; + + public PolygeneConverter( ObjectFactory objectFactory ) + { + this.objectFactory = objectFactory; + } + + /** + * Creates the marshaling {@link JsonRepresentation}. + * + * @param mediaType The target media type. + * @param source The source object to marshal. + * + * @return The marshaling {@link JsonRepresentation}. + */ + protected <T> Representation create( MediaType mediaType, T source ) + { + //noinspection unchecked + return objectFactory.newObject( JsonRepresentation.class, source ); + } + + /** + * Creates the unmarshaling {@link JsonRepresentation}. + * + * @param source The source representation to unmarshal. + * @param objectClass The object class to instantiate. + * + * @return The unmarshaling {@link JsonRepresentation}. + */ + protected <T> Representation create( Representation source, Class<T> objectClass ) + { + if( VARIANT_WWW_FORM_URLENCODED.isCompatible( source ) ) + { + return objectFactory.newObject( FormRepresentation.class, source, objectClass ); + } + else if( VARIANT_WWW_FORM_URLENCODED.isCompatible( source ) ) + { + //noinspection unchecked + return objectFactory.newObject( JsonRepresentation.class, source, objectClass ); + } + return null; + } + + public ObjectFactory getObjectFactory() + { + return objectFactory; + } + + @Override + public List<Class<?>> getObjectClasses( Variant source ) + { + List<Class<?>> result = new ArrayList<>(); + + if( isCompatible( source ) ) + { + result = addObjectClass( result, Object.class ); + result = addObjectClass( result, JsonRepresentation.class ); + } + + return result; + } + + @Override + public List<VariantInfo> getVariants( Class<?> source ) + { + List<VariantInfo> result = new ArrayList<>(); + + if( source != null ) + { + result = addVariant( result, VARIANT_JSON ); + result = addVariant( result, VARIANT_WWW_FORM_URLENCODED ); + } + + return result; + } + + /** + * Indicates if the given variant is compatible with the media types + * supported by this converter. + * + * @param variant The variant. + * + * @return True if the given variant is compatible with the media types + * supported by this converter. + */ + protected boolean isCompatible( Variant variant ) + { + //noinspection SimplifiableIfStatement + if( variant == null ) + { + return false; + } + + return VARIANT_JSON.isCompatible( variant ) || + VARIANT_WWW_FORM_URLENCODED.isCompatible( variant ) + ; + } + + @Override + public float score( Object source, Variant target, Resource resource ) + { + float result; + + if( source instanceof JsonRepresentation<?> ) + { + result = 1.0F; + } + else + { + if( target == null ) + { + result = 0.5F; + } + else if( isCompatible( target ) ) + { + result = 0.8F; + } + else + { + result = 0.5F; + } + } + + return result; + } + + @Override + public <T> float score( Representation source, Class<T> target, + Resource resource + ) + { + float result = -1.0F; + + if( source instanceof JsonRepresentation<?> ) + { + result = 1.0F; + } + else if( ( target != null ) + && JsonRepresentation.class.isAssignableFrom( target ) ) + { + result = 1.0F; + } + else if( isCompatible( source ) ) + { + result = 0.8F; + } + + return result; + } + + @SuppressWarnings( "unchecked" ) + @Override + public <T> T toObject( Representation source, Class<T> target, Resource resources ) + throws IOException + { + Object result = null; + + Representation representation = null; + if( isCompatible( source ) ) + { + representation = create( source, target ); + } + + if( representation != null ) + { + // Handle the conversion + if( ( target != null ) + && JsonRepresentation.class.isAssignableFrom( target ) ) + { + result = representation; + } + else + { + if( representation instanceof JsonRepresentation ) + { + result = ( (JsonRepresentation) representation ).getObject(); + } + if( representation instanceof FormRepresentation ) + { + result = ( (FormRepresentation) representation ).getObject(); + } + } + } + + return (T) result; + } + + @Override + public Representation toRepresentation( Object source, Variant target, Resource resource ) + { + Representation result = null; + + if( source instanceof JsonRepresentation ) + { + result = (JsonRepresentation<?>) source; + } + else + { + if( target.getMediaType() == null ) + { + target.setMediaType( MediaType.APPLICATION_JSON ); + } + if( isCompatible( target ) ) + { + result = create( target.getMediaType(), source ); + } + } + + return result; + } + + @Override + public <T> void updatePreferences( List<Preference<MediaType>> preferences, + Class<T> entity + ) + { + updatePreferences( preferences, MediaType.APPLICATION_JSON, 1.0F ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/ZestConverter.java ---------------------------------------------------------------------- diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/ZestConverter.java b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/ZestConverter.java deleted file mode 100644 index 17739b6..0000000 --- a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/ZestConverter.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.restlet.serialization; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.apache.polygene.api.injection.scope.Structure; -import org.apache.polygene.api.object.ObjectFactory; -import org.apache.polygene.spi.PolygeneSPI; -import org.restlet.data.MediaType; -import org.restlet.data.Preference; -import org.restlet.engine.converter.ConverterHelper; -import org.restlet.engine.resource.VariantInfo; -import org.restlet.representation.Representation; -import org.restlet.representation.Variant; -import org.restlet.resource.Resource; - -/** - * Converter between Apache Polygene and JSON. - */ -public class PolygeneConverter extends ConverterHelper -{ - @Structure - private PolygeneSPI spi; - - /** - * Variant with media type application/json. - */ - private static final VariantInfo VARIANT_JSON = new VariantInfo( MediaType.APPLICATION_JSON ); - private static final VariantInfo VARIANT_WWW_FORM_URLENCODED = new VariantInfo( MediaType.APPLICATION_WWW_FORM ); - - private final ObjectFactory objectFactory; - - public PolygeneConverter( ObjectFactory objectFactory ) - { - this.objectFactory = objectFactory; - } - - /** - * Creates the marshaling {@link JsonRepresentation}. - * - * @param mediaType The target media type. - * @param source The source object to marshal. - * - * @return The marshaling {@link JsonRepresentation}. - */ - protected <T> Representation create( MediaType mediaType, T source ) - { - //noinspection unchecked - return objectFactory.newObject( JsonRepresentation.class, source ); - } - - /** - * Creates the unmarshaling {@link JsonRepresentation}. - * - * @param source The source representation to unmarshal. - * @param objectClass The object class to instantiate. - * - * @return The unmarshaling {@link JsonRepresentation}. - */ - protected <T> Representation create( Representation source, Class<T> objectClass ) - { - if( VARIANT_WWW_FORM_URLENCODED.isCompatible( source ) ) - { - return objectFactory.newObject( FormRepresentation.class, source, objectClass ); - } - else if( VARIANT_WWW_FORM_URLENCODED.isCompatible( source ) ) - { - //noinspection unchecked - return objectFactory.newObject( JsonRepresentation.class, source, objectClass ); - } - return null; - } - - public ObjectFactory getObjectFactory() - { - return objectFactory; - } - - @Override - public List<Class<?>> getObjectClasses( Variant source ) - { - List<Class<?>> result = new ArrayList<>(); - - if( isCompatible( source ) ) - { - result = addObjectClass( result, Object.class ); - result = addObjectClass( result, JsonRepresentation.class ); - } - - return result; - } - - @Override - public List<VariantInfo> getVariants( Class<?> source ) - { - List<VariantInfo> result = new ArrayList<>(); - - if( source != null ) - { - result = addVariant( result, VARIANT_JSON ); - result = addVariant( result, VARIANT_WWW_FORM_URLENCODED ); - } - - return result; - } - - /** - * Indicates if the given variant is compatible with the media types - * supported by this converter. - * - * @param variant The variant. - * - * @return True if the given variant is compatible with the media types - * supported by this converter. - */ - protected boolean isCompatible( Variant variant ) - { - //noinspection SimplifiableIfStatement - if( variant == null ) - { - return false; - } - - return VARIANT_JSON.isCompatible( variant ) || - VARIANT_WWW_FORM_URLENCODED.isCompatible( variant ) - ; - } - - @Override - public float score( Object source, Variant target, Resource resource ) - { - float result; - - if( source instanceof JsonRepresentation<?> ) - { - result = 1.0F; - } - else - { - if( target == null ) - { - result = 0.5F; - } - else if( isCompatible( target ) ) - { - result = 0.8F; - } - else - { - result = 0.5F; - } - } - - return result; - } - - @Override - public <T> float score( Representation source, Class<T> target, - Resource resource - ) - { - float result = -1.0F; - - if( source instanceof JsonRepresentation<?> ) - { - result = 1.0F; - } - else if( ( target != null ) - && JsonRepresentation.class.isAssignableFrom( target ) ) - { - result = 1.0F; - } - else if( isCompatible( source ) ) - { - result = 0.8F; - } - - return result; - } - - @SuppressWarnings( "unchecked" ) - @Override - public <T> T toObject( Representation source, Class<T> target, Resource resources ) - throws IOException - { - Object result = null; - - Representation representation = null; - if( isCompatible( source ) ) - { - representation = create( source, target ); - } - - if( representation != null ) - { - // Handle the conversion - if( ( target != null ) - && JsonRepresentation.class.isAssignableFrom( target ) ) - { - result = representation; - } - else - { - if( representation instanceof JsonRepresentation ) - { - result = ( (JsonRepresentation) representation ).getObject(); - } - if( representation instanceof FormRepresentation ) - { - result = ( (FormRepresentation) representation ).getObject(); - } - } - } - - return (T) result; - } - - @Override - public Representation toRepresentation( Object source, Variant target, Resource resource ) - { - Representation result = null; - - if( source instanceof JsonRepresentation ) - { - result = (JsonRepresentation<?>) source; - } - else - { - if( target.getMediaType() == null ) - { - target.setMediaType( MediaType.APPLICATION_JSON ); - } - if( isCompatible( target ) ) - { - result = create( target.getMediaType(), source ); - } - } - - return result; - } - - @Override - public <T> void updatePreferences( List<Preference<MediaType>> preferences, - Class<T> entity - ) - { - updatePreferences( preferences, MediaType.APPLICATION_JSON, 1.0F ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneFilter.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneFilter.java b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneFilter.java new file mode 100644 index 0000000..cd76212 --- /dev/null +++ b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneFilter.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.servlet; + +import javax.servlet.Filter; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import org.apache.polygene.api.structure.Application; +import org.apache.polygene.library.servlet.lifecycle.AbstractPolygeneServletBootstrap; + +/** + * Base Filter providing easy access to the {@link Application} from the {@link ServletContext}. + * @see AbstractPolygeneServletBootstrap + */ +public abstract class PolygeneFilter + implements Filter +{ + + private Application application; + + @Override + public void init( FilterConfig filterConfig ) + throws ServletException + { + application = PolygeneServletSupport.application( filterConfig.getServletContext() ); + } + + protected final Application application() + { + return application; + } + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServlet.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServlet.java b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServlet.java new file mode 100644 index 0000000..95835a5 --- /dev/null +++ b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServlet.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.servlet; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import org.apache.polygene.api.structure.Application; +import org.apache.polygene.library.servlet.lifecycle.AbstractPolygeneServletBootstrap; + +/** + * Base HttpServlet providing easy access to the {@link org.apache.polygene.api.structure.Application} from the + * {@link javax.servlet.ServletContext}. + * + * @see AbstractPolygeneServletBootstrap + */ +public class PolygeneServlet extends HttpServlet +{ + + private Application application; + + public PolygeneServlet() + { + super(); + } + + @Override + public void init() + throws ServletException + { + super.init(); + application = PolygeneServletSupport.application( getServletContext() ); + } + + protected final Application application() + { + return application; + } + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServletSupport.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServletSupport.java b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServletSupport.java new file mode 100644 index 0000000..0b42323 --- /dev/null +++ b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/PolygeneServletSupport.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.servlet; + +import javax.servlet.ServletContext; +import org.apache.polygene.api.structure.Application; +import org.apache.polygene.library.servlet.lifecycle.AbstractPolygeneServletBootstrap; + +public final class PolygeneServletSupport +{ + + public static final String APP_IN_CTX = "polygene-application-servlet-context-attribute"; + + /** + * @param servletContext ServletContext + * @return The Application from the servlet context attribute previously set by {@link AbstractPolygeneServletBootstrap} + */ + public static Application application( ServletContext servletContext ) + { + return ( Application ) servletContext.getAttribute( APP_IN_CTX ); // TODO try/catch and find a suitable Polygene exception + } + + private PolygeneServletSupport() + { + } + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestFilter.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestFilter.java b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestFilter.java deleted file mode 100644 index cd76212..0000000 --- a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestFilter.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.servlet; - -import javax.servlet.Filter; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import org.apache.polygene.api.structure.Application; -import org.apache.polygene.library.servlet.lifecycle.AbstractPolygeneServletBootstrap; - -/** - * Base Filter providing easy access to the {@link Application} from the {@link ServletContext}. - * @see AbstractPolygeneServletBootstrap - */ -public abstract class PolygeneFilter - implements Filter -{ - - private Application application; - - @Override - public void init( FilterConfig filterConfig ) - throws ServletException - { - application = PolygeneServletSupport.application( filterConfig.getServletContext() ); - } - - protected final Application application() - { - return application; - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServlet.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServlet.java b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServlet.java deleted file mode 100644 index 95835a5..0000000 --- a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServlet.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.servlet; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import org.apache.polygene.api.structure.Application; -import org.apache.polygene.library.servlet.lifecycle.AbstractPolygeneServletBootstrap; - -/** - * Base HttpServlet providing easy access to the {@link org.apache.polygene.api.structure.Application} from the - * {@link javax.servlet.ServletContext}. - * - * @see AbstractPolygeneServletBootstrap - */ -public class PolygeneServlet extends HttpServlet -{ - - private Application application; - - public PolygeneServlet() - { - super(); - } - - @Override - public void init() - throws ServletException - { - super.init(); - application = PolygeneServletSupport.application( getServletContext() ); - } - - protected final Application application() - { - return application; - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServletSupport.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServletSupport.java b/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServletSupport.java deleted file mode 100644 index 0b42323..0000000 --- a/libraries/servlet/src/main/java/org/apache/polygene/library/servlet/ZestServletSupport.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.servlet; - -import javax.servlet.ServletContext; -import org.apache.polygene.api.structure.Application; -import org.apache.polygene.library.servlet.lifecycle.AbstractPolygeneServletBootstrap; - -public final class PolygeneServletSupport -{ - - public static final String APP_IN_CTX = "polygene-application-servlet-context-attribute"; - - /** - * @param servletContext ServletContext - * @return The Application from the servlet context attribute previously set by {@link AbstractPolygeneServletBootstrap} - */ - public static Application application( ServletContext servletContext ) - { - return ( Application ) servletContext.getAttribute( APP_IN_CTX ); // TODO try/catch and find a suitable Polygene exception - } - - private PolygeneServletSupport() - { - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PasswordDomainTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PasswordDomainTest.java b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PasswordDomainTest.java index dbbddfc..c2b3fb8 100644 --- a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PasswordDomainTest.java +++ b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PasswordDomainTest.java @@ -19,6 +19,7 @@ */ package org.apache.polygene.library.shiro; +import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.PasswordService; @@ -39,13 +40,12 @@ import org.apache.polygene.library.shiro.assembly.PasswordDomainAssembler; import org.apache.polygene.library.shiro.assembly.StandaloneShiroAssembler; import org.apache.polygene.library.shiro.domain.passwords.PasswordSecurable; import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; -import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; import static org.junit.Assert.*; public class PasswordDomainTest - extends AbstractPolygeneTest + extends AbstractPolygeneTest { // START SNIPPET: domain http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PermissionsDomainTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PermissionsDomainTest.java b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PermissionsDomainTest.java index d1911dd..604b0bb 100644 --- a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PermissionsDomainTest.java +++ b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/PermissionsDomainTest.java @@ -19,6 +19,7 @@ */ package org.apache.polygene.library.shiro; +import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.PasswordService; @@ -44,7 +45,6 @@ import org.apache.polygene.library.shiro.domain.permissions.Role; import org.apache.polygene.library.shiro.domain.permissions.RoleAssignee; import org.apache.polygene.library.shiro.domain.permissions.RoleFactory; import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; -import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; import static org.hamcrest.CoreMatchers.is; @@ -52,7 +52,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; public class PermissionsDomainTest - extends AbstractPolygeneTest + extends AbstractPolygeneTest { // START SNIPPET: domain http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/RealmServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/RealmServiceTest.java b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/RealmServiceTest.java index a02aa9f..3a774a2 100644 --- a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/RealmServiceTest.java +++ b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/RealmServiceTest.java @@ -19,6 +19,7 @@ */ package org.apache.polygene.library.shiro; +import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.DefaultPasswordService; @@ -36,13 +37,12 @@ import org.apache.polygene.bootstrap.AssemblyException; import org.apache.polygene.bootstrap.ModuleAssembly; import org.apache.polygene.library.shiro.assembly.StandaloneShiroAssembler; import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; -import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; import static org.junit.Assert.assertNotNull; public class RealmServiceTest - extends AbstractPolygeneTest + extends AbstractPolygeneTest { // START SNIPPET: realm-service http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/StandaloneShiroTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/StandaloneShiroTest.java b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/StandaloneShiroTest.java index 2c5153e..062077a 100644 --- a/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/StandaloneShiroTest.java +++ b/libraries/shiro-core/src/test/java/org/apache/polygene/library/shiro/StandaloneShiroTest.java @@ -19,6 +19,7 @@ */ package org.apache.polygene.library.shiro; +import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.IncorrectCredentialsException; @@ -38,7 +39,6 @@ import org.apache.polygene.bootstrap.ModuleAssembly; import org.apache.polygene.library.shiro.assembly.StandaloneShiroAssembler; import org.apache.polygene.library.shiro.ini.IniSecurityManagerService; import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; -import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,7 +48,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; public class StandaloneShiroTest - extends AbstractPolygeneTest + extends AbstractPolygeneTest { public void documentationSupport_before() http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebHttpShiroTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebHttpShiroTest.java b/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebHttpShiroTest.java index e2cc96c..e34edd0 100644 --- a/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebHttpShiroTest.java +++ b/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebHttpShiroTest.java @@ -19,6 +19,7 @@ */ package org.apache.polygene.library.shiro.web; +import org.apache.polygene.test.AbstractPolygeneTest; import org.junit.Test; import org.apache.polygene.api.common.Visibility; import org.apache.polygene.bootstrap.AssemblyException; @@ -27,7 +28,6 @@ import org.apache.polygene.library.http.JettyConfiguration; import org.apache.polygene.library.http.JettyServiceAssembler; import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; import org.apache.polygene.library.shiro.web.assembly.HttpShiroAssembler; -import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; import org.apache.polygene.test.util.FreePortFinder; http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebRealmServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebRealmServiceTest.java b/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebRealmServiceTest.java index 90b1be4..cebe368 100644 --- a/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebRealmServiceTest.java +++ b/libraries/shiro-web/src/test/java/org/apache/polygene/library/shiro/web/WebRealmServiceTest.java @@ -43,6 +43,7 @@ import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; +import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.shiro.authc.credential.DefaultPasswordService; import org.apache.shiro.authc.credential.PasswordMatcher; import org.apache.shiro.authc.credential.PasswordService; @@ -59,7 +60,6 @@ import org.apache.polygene.library.http.JettyConfiguration; import org.apache.polygene.library.http.JettyServiceAssembler; import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; import org.apache.polygene.library.shiro.web.assembly.HttpShiroAssembler; -import org.apache.polygene.test.AbstractPolygeneTest; import org.apache.polygene.test.EntityTestAssembler; import org.apache.polygene.test.util.FreePortFinder; http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/Constants.java ---------------------------------------------------------------------- diff --git a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/Constants.java b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/Constants.java index 503fa98..5b99275 100644 --- a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/Constants.java +++ b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/Constants.java @@ -21,7 +21,7 @@ package org.apache.polygene.library.spring.bootstrap; public final class Constants { - public static final String BEAN_ID_ZEST_APPLICATION = "polygeneApplication"; + public static final String BEAN_ID_POLYGENE_APPLICATION = "polygeneApplication"; private Constants() { http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/PolygeneApplicationBootstrap.java ---------------------------------------------------------------------- diff --git a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/PolygeneApplicationBootstrap.java b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/PolygeneApplicationBootstrap.java new file mode 100644 index 0000000..2b33e22 --- /dev/null +++ b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/PolygeneApplicationBootstrap.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.spring.bootstrap; + +import org.apache.polygene.bootstrap.ApplicationAssembly; +import org.apache.polygene.bootstrap.AssemblyException; +import org.apache.polygene.bootstrap.ModuleAssembly; +import org.springframework.context.ApplicationContextAware; + +/** + * Run a Polygene Application as a Spring Bean and export its Services to Spring. + * <p> + * Steps to export Polygene service: + * </p> + * <ul> + * <li>Create spring BeanFactory service of Apache Polygene services to export.</li> + * <li>Create a class that extends {@link PolygeneApplicationBootstrap}.</li> + * <li>Sets the layer and module that register BeanFactory service.</li> + * <li>Assemble Polygene application by implementing #assemble method.</li> + * <li>Sets the reference of bean factory service. This reference is the spring + * bean name.</li> + * <li>Declare Polygene bootstrap in spring xml application context. + * <pre><code> + * <?xml version="1.0" encoding="UTF-8"?> + * + * <beans xmlns="http://www.springframework.org/schema/beans" + * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + * xmlns:polygene="http://polygene.apache.org/schema/polygene/spring" + * xsi:schemaLocation=" + * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + * http://polygene.apache.org/schema/polygene/spring http://polygene.apache.org/schema/polygene/spring/spring-0.5.xsd"> + * + * <!-- class that implements PolygeneApplicationBootstrap --> + * + * <polygene:bootstrap class="org.apache.polygene.library.spring.bootstrap.PolygeneTestBootstrap"/> + * + * <bean id="commentServiceHolder" class="org.apache.polygene.library.spring.bootstrap.CommentServiceHolder"> + * + * <constructor-arg ref="commentService"/> <!-- Reference Polygene comment service --> + * + * </bean> + * </code></pre> + * </li> + * </ul> + * <p> + * <b>Importing Spring beans as services</b><br> + * </p> + * <ol> + * <li>Application bootstrap class must implement interface + * {@link ApplicationContextAware}.</li> + * <li>In the application bootstrap import service to the module using method + * {@link ModuleAssembly#importedServices(Class...)}.</li> + * <li>Set concrete Spring bean as meta-data of the imported service.</li> + * </ol> + * <p> + * Look at org.apache.polygene.library.spring.bootstrap.PolygeneExportServiceTest for sample + * implementation. + * </p> + */ +public abstract class PolygeneApplicationBootstrap +{ + /** + * Assembles Polygene application. + * + * @param applicationAssembly + * Polygene application assembly. Must not be {@code null}. + * @throws AssemblyException + * Thrown if assemblies fails. + */ + public abstract void assemble( ApplicationAssembly applicationAssembly ) throws AssemblyException; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/ZestApplicationBootstrap.java ---------------------------------------------------------------------- diff --git a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/ZestApplicationBootstrap.java b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/ZestApplicationBootstrap.java deleted file mode 100644 index 2b33e22..0000000 --- a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/ZestApplicationBootstrap.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.spring.bootstrap; - -import org.apache.polygene.bootstrap.ApplicationAssembly; -import org.apache.polygene.bootstrap.AssemblyException; -import org.apache.polygene.bootstrap.ModuleAssembly; -import org.springframework.context.ApplicationContextAware; - -/** - * Run a Polygene Application as a Spring Bean and export its Services to Spring. - * <p> - * Steps to export Polygene service: - * </p> - * <ul> - * <li>Create spring BeanFactory service of Apache Polygene services to export.</li> - * <li>Create a class that extends {@link PolygeneApplicationBootstrap}.</li> - * <li>Sets the layer and module that register BeanFactory service.</li> - * <li>Assemble Polygene application by implementing #assemble method.</li> - * <li>Sets the reference of bean factory service. This reference is the spring - * bean name.</li> - * <li>Declare Polygene bootstrap in spring xml application context. - * <pre><code> - * <?xml version="1.0" encoding="UTF-8"?> - * - * <beans xmlns="http://www.springframework.org/schema/beans" - * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - * xmlns:polygene="http://polygene.apache.org/schema/polygene/spring" - * xsi:schemaLocation=" - * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd - * http://polygene.apache.org/schema/polygene/spring http://polygene.apache.org/schema/polygene/spring/spring-0.5.xsd"> - * - * <!-- class that implements PolygeneApplicationBootstrap --> - * - * <polygene:bootstrap class="org.apache.polygene.library.spring.bootstrap.PolygeneTestBootstrap"/> - * - * <bean id="commentServiceHolder" class="org.apache.polygene.library.spring.bootstrap.CommentServiceHolder"> - * - * <constructor-arg ref="commentService"/> <!-- Reference Polygene comment service --> - * - * </bean> - * </code></pre> - * </li> - * </ul> - * <p> - * <b>Importing Spring beans as services</b><br> - * </p> - * <ol> - * <li>Application bootstrap class must implement interface - * {@link ApplicationContextAware}.</li> - * <li>In the application bootstrap import service to the module using method - * {@link ModuleAssembly#importedServices(Class...)}.</li> - * <li>Set concrete Spring bean as meta-data of the imported service.</li> - * </ol> - * <p> - * Look at org.apache.polygene.library.spring.bootstrap.PolygeneExportServiceTest for sample - * implementation. - * </p> - */ -public abstract class PolygeneApplicationBootstrap -{ - /** - * Assembles Polygene application. - * - * @param applicationAssembly - * Polygene application assembly. Must not be {@code null}. - * @throws AssemblyException - * Thrown if assemblies fails. - */ - public abstract void assemble( ApplicationAssembly applicationAssembly ) throws AssemblyException; -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/PolygeneNamespaceHandler.java ---------------------------------------------------------------------- diff --git a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/PolygeneNamespaceHandler.java b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/PolygeneNamespaceHandler.java new file mode 100644 index 0000000..6cbf7b4 --- /dev/null +++ b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/PolygeneNamespaceHandler.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.spring.bootstrap.internal; + +import org.apache.polygene.library.spring.bootstrap.internal.application.PolygeneBootstrapBeanDefinitionParser; +import org.apache.polygene.library.spring.bootstrap.internal.service.PolygeneServiceBeanDefinitionParser; +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + +public final class PolygeneNamespaceHandler extends NamespaceHandlerSupport +{ + @Override + public final void init() + { + registerBeanDefinitionParser( "bootstrap", new PolygeneBootstrapBeanDefinitionParser() ); + registerBeanDefinitionParser( "service", new PolygeneServiceBeanDefinitionParser() ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/ZestNamespaceHandler.java ---------------------------------------------------------------------- diff --git a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/ZestNamespaceHandler.java b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/ZestNamespaceHandler.java deleted file mode 100644 index 6cbf7b4..0000000 --- a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/ZestNamespaceHandler.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.polygene.library.spring.bootstrap.internal; - -import org.apache.polygene.library.spring.bootstrap.internal.application.PolygeneBootstrapBeanDefinitionParser; -import org.apache.polygene.library.spring.bootstrap.internal.service.PolygeneServiceBeanDefinitionParser; -import org.springframework.beans.factory.xml.NamespaceHandlerSupport; - -public final class PolygeneNamespaceHandler extends NamespaceHandlerSupport -{ - @Override - public final void init() - { - registerBeanDefinitionParser( "bootstrap", new PolygeneBootstrapBeanDefinitionParser() ); - registerBeanDefinitionParser( "service", new PolygeneServiceBeanDefinitionParser() ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/b02063bd/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/application/PolygeneApplicationFactoryBean.java ---------------------------------------------------------------------- diff --git a/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/application/PolygeneApplicationFactoryBean.java b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/application/PolygeneApplicationFactoryBean.java new file mode 100644 index 0000000..742052c --- /dev/null +++ b/libraries/spring/src/main/java/org/apache/polygene/library/spring/bootstrap/internal/application/PolygeneApplicationFactoryBean.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.polygene.library.spring.bootstrap.internal.application; + +import org.apache.polygene.api.structure.Application; +import org.apache.polygene.bootstrap.*; +import org.apache.polygene.library.spring.bootstrap.PolygeneApplicationBootstrap; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.util.Assert; + +/** + * This class responsible to handle the lifecycle of Polygene application. + */ +public final class PolygeneApplicationFactoryBean + implements FactoryBean, DisposableBean, InitializingBean, ApplicationContextAware +{ + + private final PolygeneApplicationBootstrap applicationBootstrap; + + private Application application; + + public PolygeneApplicationFactoryBean( final PolygeneApplicationBootstrap applicationBootstrap ) + { + Assert.notNull( applicationBootstrap, "'applicationBootstrap' must not be null" ); + this.applicationBootstrap = applicationBootstrap; + } + + @Override + public final Application getObject() throws Exception + { + if ( this.application == null ) + { + this.application = this.createApplication(); + } + return this.application; + } + + @Override + public final Class<Application> getObjectType() + { + return Application.class; + } + + @Override + public final boolean isSingleton() + { + return true; + } + + @Override + public final void destroy() throws Exception + { + this.getObject().passivate(); + } + + @Override + public final void afterPropertiesSet() throws Exception + { + this.getObject().activate(); + } + + private Application createApplication() + { + Energy4Java energy4Java = new Energy4Java(); + try + { + return energy4Java.newApplication( new ApplicationAssembler() + { + + @Override + public ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory ) + throws AssemblyException + { + final ApplicationAssembly applicationAssembly = applicationFactory.newApplicationAssembly(); + PolygeneApplicationFactoryBean.this.applicationBootstrap.assemble( applicationAssembly ); + return applicationAssembly; + } + } ); + } catch ( AssemblyException e ) + { + throw new BeanInitializationException( "Fail to bootstrap Polygene application.", e ); + } + + } + + @Override + public void setApplicationContext( final ApplicationContext applicationContext ) throws BeansException + { + if ( this.applicationBootstrap instanceof ApplicationContextAware ) + { + // propagate application context to the application bootstrap + ApplicationContextAware aware = (ApplicationContextAware) this.applicationBootstrap; + aware.setApplicationContext( applicationContext ); + } + } +}
