http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/service/qualifier/Qualifier.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/qualifier/Qualifier.java b/core/api/src/main/java/org/apache/zest/api/service/qualifier/Qualifier.java new file mode 100644 index 0000000..0866501 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/service/qualifier/Qualifier.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Annotation used to declare Qualifiers annotations. + */ +@Retention( RetentionPolicy.RUNTIME ) +public @interface Qualifier +{ + public abstract Class<? extends AnnotationQualifier> value(); +}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java b/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java new file mode 100644 index 0000000..3c79477 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceQualifier.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.service.qualifier; + +import org.apache.zest.api.service.ServiceReference; +import org.apache.zest.functional.Specification; + +/** + * This class helps you select a particular service + * from a list. + * <p> + * Provide a Selector which does the actual + * selection from the list. A common case is to select + * based on identity of the service, which you can do this way: + * </p> + * + * <pre><code> + * new ServiceQualifier<MyService>(services, ServiceQualifier.withId("someId")) + * </code></pre> + * <p> + * Many selectors can be combined by using firstOf. Example: + * </p> + * <pre><code> + * new ServiceQualifier<MyService>(services, firstOf(withTags("sometag"), firstActive(), first())) + * </code></pre> + * <p> + * This will pick a service that has the tag "sometag", or if none is found take the first active one. If no + * service is active, then the first service will be picked. + * </p> + */ +public abstract class ServiceQualifier +{ + public static <T> T firstService( Specification<ServiceReference<?>> qualifier, + Iterable<ServiceReference<T>> services + ) + { + for( ServiceReference<T> service : services ) + { + if( qualifier.satisfiedBy( service ) ) + { + return service.get(); + } + } + return null; + } + + public static Specification<ServiceReference<?>> withId( final String anId ) + { + return new Specification<ServiceReference<?>>() + { + @Override + public boolean satisfiedBy( ServiceReference<?> service ) + { + return service.identity().equals( anId ); + } + }; + } + + public static Specification<ServiceReference<?>> whereMetaInfoIs( final Object metaInfo ) + { + return new Specification<ServiceReference<?>>() + { + @Override + public boolean satisfiedBy( ServiceReference<?> service ) + { + Object metaObject = service.metaInfo( metaInfo.getClass() ); + return metaObject != null && metaInfo.equals( metaObject ); + } + }; + } + + public static Specification<ServiceReference<?>> whereActive() + { + return new Specification<ServiceReference<?>>() + { + @Override + public boolean satisfiedBy( ServiceReference<?> service ) + { + return service.isActive(); + } + }; + } + + public static Specification<ServiceReference<?>> whereAvailable() + { + return new Specification<ServiceReference<?>>() + { + @Override + public boolean satisfiedBy( ServiceReference<?> service ) + { + return service.isAvailable(); + } + }; + } + + public static Specification<ServiceReference<?>> withTags( final String... tags ) + { + return new Specification<ServiceReference<?>>() + { + @Override + public boolean satisfiedBy( ServiceReference<?> service ) + { + ServiceTags serviceTags = service.metaInfo( ServiceTags.class ); + + return serviceTags != null && serviceTags.hasTags( tags ); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceTags.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceTags.java b/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceTags.java new file mode 100644 index 0000000..ba627d3 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/service/qualifier/ServiceTags.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.service.qualifier; + +import java.io.Serializable; + +/** + * Use this as metainfo about a Service to specify tags. Easiest way to set them on a service + * is to use the <code>ServiceDeclaration.taggedWith(String...)</code> method. + * + * These can be used in conjunction with the withTags() Service + * Selector. + */ +public final class ServiceTags + implements Serializable +{ + private String[] tags; + + public ServiceTags( String... tags ) + { + this.tags = tags; + } + + public String[] tags() + { + return tags; + } + + public boolean hasTag( String tag ) + { + for( String serviceTag : tags ) + { + if( serviceTag.equals( tag ) ) + { + return true; + } + } + + return false; + } + + public boolean hasTags( String... aTags ) + { + for( String tag : aTags ) + { + if( !hasTag( tag ) ) + { + return false; + } + } + + return true; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/service/qualifier/Tagged.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/qualifier/Tagged.java b/core/api/src/main/java/org/apache/zest/api/service/qualifier/Tagged.java new file mode 100644 index 0000000..e2f9b2d --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/service/qualifier/Tagged.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.service.qualifier; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.apache.zest.api.service.ServiceReference; +import org.apache.zest.functional.Specification; + +/** + * Filter services based on tags. Tags can be set using the ServiceTags meta-info, like so: + * <pre><code> + * module.addService(MyService.class).taggedWith(new ServiceTags("onetag","twotag")); + * </code></pre> + * + * and then at an injection point you can do this: + * + * <pre><code> + * @Service @Tagged("onetag") MyService service; + * </code></pre> + * to get only a service tagged with MyService. If several match only the first match is used. + */ +@Retention( RetentionPolicy.RUNTIME ) +@Qualifier( Tagged.TaggedQualifier.class ) +public @interface Tagged +{ + public abstract String[] value(); + + /** + * Tagged Annotation Qualifier. + * See {@link Tagged}. + */ + public final class TaggedQualifier + implements AnnotationQualifier<Tagged> + { + @Override + public Specification<ServiceReference<?>> qualifier( Tagged tagged ) + { + return ServiceQualifier.withTags( tagged.value() ); + } + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/service/qualifier/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/qualifier/package.html b/core/api/src/main/java/org/apache/zest/api/service/qualifier/package.html new file mode 100644 index 0000000..ec0edd3 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/service/qualifier/package.html @@ -0,0 +1,59 @@ +<!-- +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. +--> +<html> + <body> + <h2>Service Qualifiers.</h2> + <p> + The @Service injection is only able to specify the type of the service to be injected. If any other type of + qualification has to be done it has to be done manually but for common cases it's more convenient to use + annotations to do this filtering. This package contains annotations to perform this qualification. + </p> + <p>Example:</p> + <blockquote> + <pre>@Service @Tagged( "sometag" ) MyService service;</pre> + </blockquote> + <p> + This will only inject instances of MyService that have been tagged with "sometag". If none exist an + exception will occur at injection time since it is not optional. + </p> + <p>It also works with iterables:</p> + <blockquote> + <pre>@Service @Tagged( "sometag" ) Iterable<MyService> services;</pre> + </blockquote> + <p> + The qualification will be evaluated upon each call to iterator(), and since the qualifier has access to a + ServiceReference, which contains the isActive() method, it can even provide some dynamicity. + </p> + <blockquote> + <pre>@Service @Active Iterable<SomeImportedService> importedServices;</pre> + </blockquote> + <p> + Let's say these SomeImportedService are only sometimes available. Then whenever iterator() is called the + {@link org.qi4j.api.service.qualifier.Active} tag can kick in and filter out those whose + ServiceReference.isActive() returns false. + </p> + <p>Standard ones defined in the API are:</p> + <ul> + <li>{@link org.qi4j.api.service.qualifier.Active}</li> + <li>{@link org.qi4j.api.service.qualifier.Available}</li> + <li>{@link org.qi4j.api.service.qualifier.HasMetaInfo}</li> + <li>{@link org.qi4j.api.service.qualifier.IdentifiedBy}</li> + <li>{@link org.qi4j.api.service.qualifier.Tagged}</li> + </ul> + <p>See tests and API for more examples, and how to implement your own qualifiers.</p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/GenericSideEffect.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/GenericSideEffect.java b/core/api/src/main/java/org/apache/zest/api/sideeffect/GenericSideEffect.java new file mode 100644 index 0000000..897eea2 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/GenericSideEffect.java @@ -0,0 +1,61 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * 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.apache.zest.api.sideeffect; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + * Base class for generic SideEffects. + */ +public abstract class GenericSideEffect + extends SideEffectOf<InvocationHandler> + implements InvocationHandler +{ + + /** + * {@inheritDoc} + */ + @Override + public Object invoke( final Object proxy, final Method method, final Object[] args ) + throws Throwable + { + invoke( method, args ); + return null; + } + + /** + * Convenience method to be overridden by subclasses in order to avoid returning null, as returned value from side + * effects is not taken in consideration. + * + * @param method the method that was invoked + * @param args the arguments of the method invocation + * + * @throws Throwable - the exception to throw from the method invocation on the proxy instance. The exception's type + * must be assignable either to any of the exception types declared in the throws clause of the + * interface method or to the unchecked exception types {code}java.lang.RuntimeException{code} + * or {code}java.lang.Error{code}. If a checked exception is thrown by this method that is not + * assignable to any of the exception types declared in the throws clause of the interface method, + * then an UndeclaredThrowableException containing the exception that was thrown by this method + * will be thrown by the method invocation on the proxy instance. + */ + protected void invoke( final Method method, final Object[] args ) + throws Throwable + { + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectDescriptor.java b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectDescriptor.java new file mode 100644 index 0000000..2f573e5 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.sideeffect; + +/** + * SideEffect Descriptor. + */ +public interface SideEffectDescriptor +{ + Class<?> modifierClass(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectOf.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectOf.java b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectOf.java new file mode 100644 index 0000000..82812e8 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectOf.java @@ -0,0 +1,38 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * 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.apache.zest.api.sideeffect; + +import org.apache.zest.api.sideeffect.internal.SideEffectFor; + +/** + * Base class for SideEffects. It introduces a typed "next" pointer + * that SideEffects can use to get the result of the original invocation. + * <p> + * Generic SideEffects should subclass {@link GenericSideEffect} instead. + * </p> + * <p> + * SideEffects implementations must be thread-safe in their implementation, + * as multiple threads may share instances. + * </p> + */ +public abstract class SideEffectOf<T> +{ + final + @SideEffectFor + protected T result = null; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffects.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffects.java b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffects.java new file mode 100644 index 0000000..f34ea01 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffects.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.sideeffect; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation is used by composites and mixins to declare what SideEffects + * should apply to the type or specific method. + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( { ElementType.TYPE, ElementType.METHOD } ) +@Documented +public @interface SideEffects +{ + Class<?>[] value(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectsDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectsDescriptor.java b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectsDescriptor.java new file mode 100644 index 0000000..9c9e64e --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/SideEffectsDescriptor.java @@ -0,0 +1,24 @@ +/* Copyright 2008 Edward Yakop. +* +* 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.apache.zest.api.sideeffect; + +/** + * SideEffects Descriptor. + */ +public interface SideEffectsDescriptor +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/SideEffectFor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/SideEffectFor.java b/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/SideEffectFor.java new file mode 100644 index 0000000..f3f16b2 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/SideEffectFor.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.sideeffect.internal; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.apache.zest.api.injection.InjectionScope; + +/** + * This annotation is required once in each SideEffect, to mark the + * field where the element providing the invocation result should be + * injected. + * <p> + * The type of the field must be of the same type as the SideEffect + * itself, or an InvocationHandler. + * </p> + * <p> + * Example; + * </p> + * <pre><code> + * public interface MyStuff + * { + * SomeResult doSomething(); + * } + * + * public class MyStuffSideEffect + * implements MyStuff + * { + * @SideEffectFor MyStuff next; + * + * public SomeResult doSomething() + * { + * SomeResult result = next.doSomething(); + * + * // HERE DO THE SIDEEFFECT STUFF. + * + * return result; // Result value is ignored, null would work too. + * } + * } + * </code></pre> + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.FIELD, ElementType.PARAMETER }) +@Documented +@InjectionScope +public @interface SideEffectFor +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/package.html b/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/package.html new file mode 100644 index 0000000..269774e --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/internal/package.html @@ -0,0 +1,25 @@ +<!-- +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. +--> +<html> + <body> + <h1>Internal/Private package for the API.</h1> + <p> + This is an internal package, and no classes in this package is part of the API and compatibility + with these classes will not be attempted. + </p> + </body> +</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/sideeffect/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/sideeffect/package.html b/core/api/src/main/java/org/apache/zest/api/sideeffect/package.html new file mode 100644 index 0000000..a658adb --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/sideeffect/package.html @@ -0,0 +1,21 @@ +<!-- +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. +--> +<html> + <body> + <h2>SideEffect API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/Application.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/Application.java b/core/api/src/main/java/org/apache/zest/api/structure/Application.java new file mode 100644 index 0000000..1786d11 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/Application.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2008, Niclas Hedhman. + * + * 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.apache.zest.api.structure; + +import org.apache.zest.api.activation.Activation; +import org.apache.zest.api.activation.ActivationEventListenerRegistration; + +/** + * The Application represents a whole Zest application. + */ +public interface Application + extends ActivationEventListenerRegistration, Activation, MetaInfoHolder +{ + /** + * Application modes. + */ + public enum Mode + { + /** + * Should be used for unit test runs. Created files etc. should be cleaned up between runs. + */ + test, + /** + * Should be used during development. Typically create in-memory databases etc. + */ + development, + /** + * Should be used in QA environments, and other production-like settings where different set of external + * resources are utilized. + */ + staging, + /** + * Should be used in production. All databases are persistent on disk etc. + */ + production + } + + /** + * @return Application name + */ + String name(); + + /** + * The version of the application. This can be in any format, but + * most likely will follow the Dewey format, i.e. x.y.z. + * + * @return the version of the application + */ + String version(); + + /** + * @return Application Mode + */ + Mode mode(); + + /** + * Find a Layer. + * + * @param layerName Layer name + * @return Found Layer, never returns null + * @throws IllegalArgumentException if there's no such Layer + */ + Layer findLayer( String layerName ) + throws IllegalArgumentException; + + /** + * Find a Module. + * + * @param layerName Layer name + * @param moduleName Module name + * @return Found Module, never returns null + * @throws IllegalArgumentException if there's no such Module + */ + Module findModule( String layerName, String moduleName ) + throws IllegalArgumentException; + + /** + * @return Application Descriptor + */ + ApplicationDescriptor descriptor(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/ApplicationDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/ApplicationDescriptor.java b/core/api/src/main/java/org/apache/zest/api/structure/ApplicationDescriptor.java new file mode 100644 index 0000000..9d89ebd --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/ApplicationDescriptor.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.structure; + +import org.apache.zest.api.Qi4j; +import org.apache.zest.functional.VisitableHierarchy; + +/** + * Application Descriptor. + */ +public interface ApplicationDescriptor + extends VisitableHierarchy<Object, Object> +{ + /** + * Create a new instance of the Application. + * @param runtime Zest Runtime + * @param importedServiceInstances Imported Services instances + * @return a new instance of the Application. + */ + Application newInstance( Qi4j runtime, Object... importedServiceInstances ); + + /** + * @return the Application's name + */ + String name(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/Layer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/Layer.java b/core/api/src/main/java/org/apache/zest/api/structure/Layer.java new file mode 100644 index 0000000..2f6c28e --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/Layer.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2008, Niclas Hedhman. + * + * 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.apache.zest.api.structure; + +import org.apache.zest.api.activation.Activation; +import org.apache.zest.api.activation.ActivationEventListenerRegistration; + +/** + * The Layer represents a single layer in a Zest application. + */ +public interface Layer + extends ActivationEventListenerRegistration, Activation, MetaInfoHolder +{ + /** + * @return the Layer's name + */ + String name(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/LayerDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/LayerDescriptor.java b/core/api/src/main/java/org/apache/zest/api/structure/LayerDescriptor.java new file mode 100644 index 0000000..812b13f --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/LayerDescriptor.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.structure; + +/** + * Layer Descriptor. + */ +public interface LayerDescriptor +{ + + /** + * @return the Layer's name + */ + String name(); + + /** + * @return Layers used by this Layer + */ + UsedLayersDescriptor usedLayers(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/MetaInfoHolder.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/MetaInfoHolder.java b/core/api/src/main/java/org/apache/zest/api/structure/MetaInfoHolder.java new file mode 100644 index 0000000..cbaf7df --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/MetaInfoHolder.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2012, Niclas Hedhman. All Rights Reserved. + * + * 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.apache.zest.api.structure; + +/** + * MetaInfo holder. + */ +public interface MetaInfoHolder +{ + + /** + * Get metadata that implements the given type. + * The info is registered during assembly of the application. + * + * @param infoType the type of metadata to be returned + * + * @return the metadata for the given type, or <code>null</code> if + * no such metadata has been registered + */ + <T> T metaInfo( Class<T> infoType ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/Module.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/Module.java b/core/api/src/main/java/org/apache/zest/api/structure/Module.java new file mode 100644 index 0000000..09842c2 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/Module.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2008, Niclas Hedhman. + * + * 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.apache.zest.api.structure; + +import org.apache.zest.api.activation.ActivationEventListenerRegistration; +import org.apache.zest.api.composite.TransientBuilderFactory; +import org.apache.zest.api.composite.TransientDescriptor; +import org.apache.zest.api.entity.EntityDescriptor; +import org.apache.zest.api.injection.scope.Structure; +import org.apache.zest.api.object.ObjectDescriptor; +import org.apache.zest.api.object.ObjectFactory; +import org.apache.zest.api.query.QueryBuilderFactory; +import org.apache.zest.api.service.ServiceFinder; +import org.apache.zest.api.unitofwork.UnitOfWorkFactory; +import org.apache.zest.api.value.ValueBuilderFactory; +import org.apache.zest.api.value.ValueDescriptor; + +/** + * API for interacting with a Module. Instances + * of this can be accessed by using the {@link Structure} + * injection scope. + */ +public interface Module + extends ActivationEventListenerRegistration, + MetaInfoHolder, + ObjectFactory, + TransientBuilderFactory, + ValueBuilderFactory, + UnitOfWorkFactory, + QueryBuilderFactory, + ServiceFinder +{ + + /** + * @return the Module's name + */ + String name(); + + /** + * @return the Module's ClassLoader + */ + ClassLoader classLoader(); + + /** + * @param typeName name of a transient composite type + * @return the descriptor for a transient composite or null if the class could not be found or the transient composite is not visible + */ + TransientDescriptor transientDescriptor( String typeName ); + + /** + * @param typeName name of an entity composite type + * @return the descriptor for an entity composite or null if the class could not be found or the entity composite is not visible + */ + EntityDescriptor entityDescriptor( String typeName ); + + /** + * @param typeName name of an object type + * @return the descriptor for an object or null if the class could not be found or the object is not visible + */ + ObjectDescriptor objectDescriptor( String typeName ); + + /** + * @param typeName name of a value composite type + * @return the descriptor for a value composite or null if the class could not be found or the value composite is not visible + */ + ValueDescriptor valueDescriptor( String typeName ); + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/ModuleDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/ModuleDescriptor.java b/core/api/src/main/java/org/apache/zest/api/structure/ModuleDescriptor.java new file mode 100644 index 0000000..045a639 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/ModuleDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.structure; + +/** + * Module Descriptor. + */ +public interface ModuleDescriptor +{ + String name(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/UsedLayersDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/UsedLayersDescriptor.java b/core/api/src/main/java/org/apache/zest/api/structure/UsedLayersDescriptor.java new file mode 100644 index 0000000..98e70d5 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/UsedLayersDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.structure; + +/** + * Used Layers Descriptor. + */ +public interface UsedLayersDescriptor +{ + Iterable<? extends LayerDescriptor> layers(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/structure/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/structure/package.html b/core/api/src/main/java/org/apache/zest/api/structure/package.html new file mode 100644 index 0000000..3134a90 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/structure/package.html @@ -0,0 +1,21 @@ +<!-- +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. +--> +<html> + <body> + <h2>Application Structure API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/CollectionType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/CollectionType.java b/core/api/src/main/java/org/apache/zest/api/type/CollectionType.java new file mode 100644 index 0000000..872c07d --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/CollectionType.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import org.apache.zest.api.util.Classes; + +/** + * Collection ValueType. + * <p>This handles Collection, List and Set types.</p> + */ +public final class CollectionType + extends ValueType +{ + + public static boolean isCollection( Type type ) + { + Class<?> cl = Classes.RAW_CLASS.map( type ); + return cl.equals( Collection.class ) || cl.equals( List.class ) || cl.equals( Set.class ); + } + + public static CollectionType collectionOf( Class<?> collectedType ) + { + return new CollectionType( Collection.class, ValueType.of( collectedType ) ); + } + + public static CollectionType listOf( Class<?> collectedType ) + { + return new CollectionType( List.class, ValueType.of( collectedType ) ); + } + + public static CollectionType setOf( Class<?> collectedType ) + { + return new CollectionType( Set.class, ValueType.of( collectedType ) ); + } + private ValueType collectedType; + + public CollectionType( Class<?> type, ValueType collectedType ) + { + super( type ); + this.collectedType = collectedType; + if( !isCollection( type ) ) + { + throw new IllegalArgumentException( type + " is not a Collection, List or Set." ); + } + } + + public ValueType collectedType() + { + return collectedType; + } + + @Override + public String toString() + { + return super.toString() + "<" + collectedType + ">"; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/EnumType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/EnumType.java b/core/api/src/main/java/org/apache/zest/api/type/EnumType.java new file mode 100644 index 0000000..692ab53 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/EnumType.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import java.lang.reflect.Type; + +/** + * Enum ValueType. + */ +public final class EnumType + extends ValueType +{ + + public static boolean isEnum( Type type ) + { + if( type instanceof Class ) + { + Class<?> typeClass = (Class) type; + return ( typeClass.isEnum() ); + } + return false; + } + + public static EnumType of( Class<?> type ) + { + return new EnumType( type ); + } + + public EnumType( Class<?> type ) + { + super( type ); + if( !isEnum( type ) ) + { + throw new IllegalArgumentException( type + " is not an Enum." ); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/HasTypes.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/HasTypes.java b/core/api/src/main/java/org/apache/zest/api/type/HasTypes.java new file mode 100644 index 0000000..862b7eb --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/HasTypes.java @@ -0,0 +1,27 @@ +/* + * 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.zest.api.type; + +/** + * Has types. + */ +public interface HasTypes +{ + Iterable<Class<?>> types(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/MapType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/MapType.java b/core/api/src/main/java/org/apache/zest/api/type/MapType.java new file mode 100644 index 0000000..f2baf77 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/MapType.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import java.lang.reflect.Type; +import java.util.Map; +import org.apache.zest.api.util.Classes; + +/** + * Map ValueType. + * <p>This handles instances of Map.</p> + */ +public final class MapType + extends ValueType +{ + + private ValueType keyType; + private ValueType valueType; + private final Serialization.Variant variant; + + public static boolean isMap( Type type ) + { + Class<?> cl = Classes.RAW_CLASS.map( type ); + return Map.class.isAssignableFrom( cl ); + } + + public static MapType of( Class<?> keyType, Class<?> valueType ) + { + return new MapType( Map.class, ValueType.of( keyType ), ValueType.of( valueType ) ); + } + + public static MapType of( Class<?> keyType, Class<?> valueType, Serialization.Variant variant ) + { + return new MapType( Map.class, ValueType.of( keyType ), ValueType.of( valueType ), variant ); + } + + public MapType( Class<?> type, ValueType keyType, ValueType valueType ) + { + this( type, keyType, valueType, Serialization.Variant.entry ); + } + + public MapType( Class<?> type, ValueType keyType, ValueType valueType, Serialization.Variant variant ) + { + super( type ); + this.keyType = keyType; + this.valueType = valueType; + this.variant = variant; + if( !isMap( type ) ) + { + throw new IllegalArgumentException( type + " is not a Map." ); + } + } + + public ValueType keyType() + { + return keyType; + } + + public ValueType valueType() + { + return valueType; + } + + public Serialization.Variant variant() + { + return variant; + } + + @Override + public String toString() + { + return super.toString() + "<" + keyType + "," + valueType + ">"; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/MatchTypeSpecification.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/MatchTypeSpecification.java b/core/api/src/main/java/org/apache/zest/api/type/MatchTypeSpecification.java new file mode 100644 index 0000000..328dcf4 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/MatchTypeSpecification.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2012, Niclas Hedhman. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import org.apache.zest.functional.Specification; + +/** + * Match Type Specification for HasTypes. + */ +public class MatchTypeSpecification + implements Specification<HasTypes> +{ + private final Class<?> matchType; + + public MatchTypeSpecification( Class<?> matchType ) + { + this.matchType = matchType; + } + + @Override + public boolean satisfiedBy( HasTypes item ) + { + for( Class<?> type : item.types() ) + { + if( matchType.isAssignableFrom( type ) ) + { + return true; + } + } + return false; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/Serialization.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/Serialization.java b/core/api/src/main/java/org/apache/zest/api/type/Serialization.java new file mode 100644 index 0000000..c0d70e6 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/Serialization.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Serialization options for Property intstances. + * <p> + * The {@code entry} type represents the explicit key=keyValue, value=valueValue. For JSON serialization; + * </p> + * <pre> + * [ + * { "key1" : "value1" }, + * { "key2" : "value2" } + * ] + * </pre> + * <p> + * For XML serialization; + * </p> + * <pre> + * <object> + * < + * </object> + * </pre> + * <p> + * The {@code object} type represents the explicit keyValue=valueValue. + * </p> + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( { ElementType.TYPE, ElementType.METHOD } ) +@Documented +public @interface Serialization +{ + Variant value(); + + enum Variant + { + entry, object + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/ValueCompositeType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/ValueCompositeType.java b/core/api/src/main/java/org/apache/zest/api/type/ValueCompositeType.java new file mode 100644 index 0000000..67cc7bf --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/ValueCompositeType.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import java.lang.reflect.Type; +import org.apache.zest.api.association.AssociationDescriptor; +import org.apache.zest.api.property.PropertyDescriptor; +import org.apache.zest.api.util.Classes; +import org.apache.zest.api.value.ValueComposite; +import org.apache.zest.api.value.ValueDescriptor; + +/** + * ValueComposite ValueType. + */ +public final class ValueCompositeType + extends ValueType +{ + private final ValueDescriptor model; + + public static boolean isValueComposite( Type type ) + { + return ValueComposite.class.isAssignableFrom( Classes.RAW_CLASS.map( type ) ); + } + + public ValueCompositeType( ValueDescriptor model ) + { + super( model.types() ); + this.model = model; + } + + public Iterable<? extends PropertyDescriptor> properties() + { + return model.state().properties(); + } + + public Iterable<? extends AssociationDescriptor> associations() + { + return model.state().associations(); + } + + public Iterable<? extends AssociationDescriptor> manyAssociations() + { + return model.state().manyAssociations(); + } + + public Iterable<? extends AssociationDescriptor> namedAssociations() + { + return model.state().namedAssociations(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/ValueType.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/ValueType.java b/core/api/src/main/java/org/apache/zest/api/type/ValueType.java new file mode 100644 index 0000000..a13b509 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/ValueType.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2013, Paul Merlin. All Rights Reserved. + * + * 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.apache.zest.api.type; + +import java.util.Collections; +import org.apache.zest.api.util.NullArgumentException; +import org.apache.zest.functional.Function; +import org.apache.zest.functional.Iterables; + +import static org.apache.zest.functional.Iterables.first; + +/** + * Base class for types of values in ValueComposites and Properties. + */ +public class ValueType + implements HasTypes +{ + + public static ValueType of( Class<?> type ) + { + return new ValueType( type ); + } + + /** + * Check if a non-null object is of any of the Primitive Value Types or an array of them. + * <p> + * String, Boolean, Integer, Double, Float, Long, Byte, Short and Character and their Java primitive types + * counterparts are considered as Primitive Value Types. + * </p> + * <p> + * Date, BigInteger, BigDecimal and JodaTime types are not considered as Primitive Value Types. + * </p> + * + * @return true if object is a primitive value or an array of primitive values + * @throws IllegalArgumentException if object is null + */ + public static boolean isPrimitiveValue( Object object ) + { + NullArgumentException.validateNotNull( "object", object ); + if( object instanceof String + || object instanceof Character + || object instanceof Boolean + || object instanceof Integer + || object instanceof Double + || object instanceof Float + || object instanceof Long + || object instanceof Byte + || object instanceof Short ) + { + return true; + } + if( object.getClass().isArray() ) + { + return isArrayOfPrimitiveValues( object ); + } + return false; + } + + private static boolean isArrayOfPrimitiveValues( Object array ) + { + if( array instanceof String[] + || array instanceof char[] || array instanceof Character[] + || array instanceof boolean[] || array instanceof Boolean[] + || array instanceof int[] || array instanceof Integer[] + || array instanceof double[] || array instanceof Double[] + || array instanceof float[] || array instanceof Float[] + || array instanceof long[] || array instanceof Long[] + || array instanceof byte[] || array instanceof Byte[] + || array instanceof short[] || array instanceof Short[] ) + { + return true; + } + return false; + } + + public static boolean isPrimitiveValueType( ValueType valueType ) + { + return isPrimitiveValueType( valueType.mainType() ); + } + + /** + * @see ValueType#isPrimitiveValue(java.lang.Object) + */ + public static boolean isPrimitiveValueType( Class<?> type ) + { + NullArgumentException.validateNotNull( "type", type ); + if( String.class.isAssignableFrom( type ) ) + { + return true; + } + if( type.isArray() ) + { + return isPrimitiveValueType( type.getComponentType() ); + } + return false; + } + protected final Iterable<Class<?>> types; + + public ValueType( Class<?> type ) + { + this( Collections.singleton( type ) ); + } + + @SuppressWarnings( "unchecked" ) + public ValueType( Iterable<? extends Class<?>> types ) + { + this.types = (Iterable<Class<?>>) types; + } + + public Class<?> mainType() + { + return first( types ); + } + + @Override + public Iterable<Class<?>> types() + { + return types; + } + + @Override + public String toString() + { + String name = Iterables.toString( + types, + new Function<Class<?>, String>() + { + @Override + public String map( Class<?> item ) + { + return item.getName(); + } + }, + "," ); + if( name.contains( "," ) ) + { + name = "{" + name + "}"; + } + return name; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/type/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/type/package.html b/core/api/src/main/java/org/apache/zest/api/type/package.html new file mode 100644 index 0000000..d42baa3 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/type/package.html @@ -0,0 +1,21 @@ +<!-- +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. +--> +<html> + <body> + <h2>Type API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/unitofwork/ConcurrentEntityModificationException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/unitofwork/ConcurrentEntityModificationException.java b/core/api/src/main/java/org/apache/zest/api/unitofwork/ConcurrentEntityModificationException.java new file mode 100644 index 0000000..5d02845 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/unitofwork/ConcurrentEntityModificationException.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * 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.apache.zest.api.unitofwork; + +import org.apache.zest.api.entity.EntityComposite; + +/** + * This exception is thrown by UnitOfWork.complete() if any entities that are being committed + * had been changed while the UnitOfWork was being executed. + */ +public class ConcurrentEntityModificationException + extends UnitOfWorkCompletionException +{ + private static final long serialVersionUID = 3872723845064767689L; + + private final Iterable<EntityComposite> concurrentlyModifiedEntities; + + public ConcurrentEntityModificationException( Iterable<EntityComposite> concurrentlyModifiedEntities ) + { + super("Entities changed concurrently :" + concurrentlyModifiedEntities); + this.concurrentlyModifiedEntities = concurrentlyModifiedEntities; + } + + public Iterable<EntityComposite> concurrentlyModifiedEntities() + { + return concurrentlyModifiedEntities; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityCompositeAlreadyExistsException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityCompositeAlreadyExistsException.java b/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityCompositeAlreadyExistsException.java new file mode 100644 index 0000000..0872ba6 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityCompositeAlreadyExistsException.java @@ -0,0 +1,43 @@ +/* + * Copyright 2008 Niclas Hedhman. + * + * 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.apache.zest.api.unitofwork; + +import org.apache.zest.api.entity.EntityReference; + +/** + * If you try to create an EntityComposite whose identity already exists, + * then this exception will be thrown. + */ +public class EntityCompositeAlreadyExistsException + extends UnitOfWorkException +{ + private static final long serialVersionUID = -7297710939536508481L; + + private final EntityReference identity; + + public EntityCompositeAlreadyExistsException( EntityReference identity ) + { + super( "EntityComposite (" + identity + ") already exists." ); + this.identity = identity; + } + + public EntityReference identity() + { + return identity; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityTypeNotFoundException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityTypeNotFoundException.java b/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityTypeNotFoundException.java new file mode 100644 index 0000000..58da2e1 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/unitofwork/EntityTypeNotFoundException.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2007-2008, Niclas Hedhman. All Rights Reserved. + * Copyright (c) 2007, Alin Dreghiciu. All Rights Reserved. + * + * 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.apache.zest.api.unitofwork; + +import org.apache.zest.functional.Function; + +import static org.apache.zest.functional.Iterables.fold; + +/** + * Zest exception to be thrown in case that an entity composite + * was not found during a lookup call. + */ +public class EntityTypeNotFoundException + extends UnitOfWorkException +{ + private final String compositeType; + + public EntityTypeNotFoundException( String entityType, String moduleName, Iterable<String> visibility ) + { + super( "Could not find an EntityComposite of type " + entityType + " in module [" + moduleName + "].\n" + + "\tThe following entity types are visible:\n" + join(visibility) ); + this.compositeType = entityType; + } + + private static String join( Iterable<String> visibility ) + { + return fold( new Function<String, String>() + { + StringBuilder result; + { + result = new StringBuilder(); + } + + @Override + public String map( String type ) + { + result.append( type ); + result.append( "\n" ); + return result.toString(); + } + }, visibility ); + } + + public String compositeType() + { + return compositeType; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/8744a67f/core/api/src/main/java/org/apache/zest/api/unitofwork/NoSuchEntityException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/unitofwork/NoSuchEntityException.java b/core/api/src/main/java/org/apache/zest/api/unitofwork/NoSuchEntityException.java new file mode 100644 index 0000000..297344b --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/unitofwork/NoSuchEntityException.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2008-2013, Niclas Hedhman. All Rights Reserved. + * + * 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.apache.zest.api.unitofwork; + +import org.apache.zest.api.entity.EntityReference; +import org.apache.zest.api.usecase.Usecase; +import org.apache.zest.functional.Function; +import org.apache.zest.functional.Iterables; + +/** + * This exception indicates that the requested Entity with the given + * identity does not exist. + */ +public class NoSuchEntityException + extends UnitOfWorkException +{ + private final EntityReference identity; + private final Usecase usecase; + private final Class<?>[] mixinTypes; + + public NoSuchEntityException( EntityReference identity, Class<?> mixinType, Usecase usecase ) + { + super( "Could not find entity (" + identity + ") of type " + mixinType.getName() + " in usecase '" + usecase.name() + "'" ); + this.identity = identity; + this.usecase = usecase; + this.mixinTypes = new Class<?>[]{ mixinType }; + } + + public NoSuchEntityException( EntityReference identity, Class<?>[] mixinTypes, Usecase usecase ) + { + super( "Could not find entity (" + identity + ") of type " + toString( mixinTypes ) + " in usecase '" + usecase.name() + "'" ); + this.identity = identity; + this.mixinTypes = mixinTypes; + this.usecase = usecase; + } + + public NoSuchEntityException( EntityReference identity, Iterable<Class<?>> types, Usecase usecase ) + { + this( identity, castToArray( types ), usecase ); + } + + public EntityReference identity() + { + return identity; + } + + public Class<?>[] mixinTypes() + { + return mixinTypes; + } + + public Usecase usecase() + { + return usecase; + } + + private static Class<?>[] castToArray( Iterable<Class<?>> iterableClasses ) + { + Iterable<Class> types = Iterables.cast( iterableClasses ); + return Iterables.toArray( Class.class, types ); + } + + private static String toString( Class<?>[] mixinTypes ) + { + Iterable<String> map = Iterables.map( new Function<Class<?>, String>() + { + @Override + public String map( Class<?> item ) + { + return item.getName(); + } + }, Iterables.iterable( mixinTypes ) ); + return Iterables.fold( new Function<String, String>() + { + StringBuilder result; + boolean first = true; + + { + result = new StringBuilder(); + result.append( "[" ); + } + + @Override + public String map( String strings ) + { + if( !first ) + { + result.append( ',' ); + } + first = false; + result.append( strings ); + return result.toString() + "]"; + } + }, map ); + } +}
