http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/ManyAssociationFunction.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/ManyAssociationFunction.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/ManyAssociationFunction.java deleted file mode 100644 index 1c63840..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/ManyAssociationFunction.java +++ /dev/null @@ -1,118 +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.zest.api.query.grammar; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Member; -import java.lang.reflect.Proxy; -import java.util.function.Function; -import org.apache.zest.api.association.AssociationStateHolder; -import org.apache.zest.api.association.ManyAssociation; -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.composite.CompositeInstance; - -/** - * Function to get Entity ManyAssociations. - */ -public class ManyAssociationFunction<T> - implements Function<Composite, ManyAssociation<T>> -{ - private final AssociationFunction<?> traversedAssociation; - private final ManyAssociationFunction<?> traversedManyAssociation; - private final NamedAssociationFunction<?> traversedNamedAssociation; - private final AccessibleObject accessor; - - public ManyAssociationFunction( AssociationFunction<?> traversedAssociation, - ManyAssociationFunction<?> traversedManyAssociation, - NamedAssociationFunction<?> traversedNamedAssociation, - AccessibleObject accessor - ) - { - this.traversedAssociation = traversedAssociation; - this.traversedManyAssociation = traversedManyAssociation; - this.traversedNamedAssociation = traversedNamedAssociation; - this.accessor = accessor; - } - - public AssociationFunction<?> traversedAssociation() - { - return traversedAssociation; - } - - public ManyAssociationFunction<?> traversedManyAssociation() - { - return traversedManyAssociation; - } - - public NamedAssociationFunction<?> traversedNamedAssociation() - { - return traversedNamedAssociation; - } - - public AccessibleObject accessor() - { - return accessor; - } - - @Override - public ManyAssociation<T> apply( Composite entity ) - { - try - { - Object target = entity; - if( traversedAssociation != null ) - { - target = traversedAssociation.apply( entity ).get(); - } - if( traversedManyAssociation != null ) - { - throw new IllegalArgumentException( "Cannot traverse ManyAssociations" ); - } - if( traversedNamedAssociation != null ) - { - throw new IllegalArgumentException( "Cannot traverse NamedAssociations" ); - } - - CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler( target ); - return ( (AssociationStateHolder) handler.state() ).manyAssociationFor( accessor ); - } - catch( IllegalArgumentException e ) - { - throw e; - } - catch( Throwable e ) - { - throw new IllegalArgumentException( e ); - } - } - - @Override - public String toString() - { - if( traversedAssociation != null ) - { - return traversedAssociation.toString() + "." + ( (Member) accessor ).getName(); - } - else - { - return ( (Member) accessor ).getName(); - } - } -}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/MatchesPredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/MatchesPredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/MatchesPredicate.java deleted file mode 100644 index ce8f7b3..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/MatchesPredicate.java +++ /dev/null @@ -1,86 +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.zest.api.query.grammar; - -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.property.Property; - -/** - * Regular expression match Specification. - */ -public class MatchesPredicate - extends ExpressionPredicate -{ - private PropertyFunction<String> property; - private Object value; - - public MatchesPredicate( PropertyFunction<String> property, String regexp ) - { - this.property = property; - this.value = regexp; - } - - public MatchesPredicate( PropertyFunction<String> property, Variable variable ) - { - this.property = property; - this.value = variable; - } - - public PropertyFunction<String> property() - { - return property; - } - - public Object value() - { - return value; - } - - public String regexp() - { - return ( String ) value; - } - - @Override - public boolean test( Composite item ) - { - Property<String> prop = property.apply( item ); - - if( prop == null ) - { - return false; - } - - String val = prop.get(); - - if( val == null ) - { - return false; - } - - return val.matches( ( String ) value ); - } - - @Override - public String toString() - { - return "( " + property + " matches " + "\"" + value + "\"" + " )"; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsNamePredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsNamePredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsNamePredicate.java deleted file mode 100644 index 400b9eb..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsNamePredicate.java +++ /dev/null @@ -1,66 +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.zest.api.query.grammar; - -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.composite.Composite; - -/** - * NamedAssociation Contains Specification. - */ -public class NamedAssociationContainsNamePredicate<T> - extends ExpressionPredicate -{ - private final NamedAssociationFunction<T> namedAssociationFunction; - private final String name; - - public NamedAssociationContainsNamePredicate( NamedAssociationFunction<T> namedAssociationFunction, String name ) - { - this.namedAssociationFunction = namedAssociationFunction; - this.name = name; - } - - public NamedAssociationFunction<T> namedAssociation() - { - return namedAssociationFunction; - } - - public String name() - { - return name; - } - - @Override - public boolean test( Composite item ) - { - NamedAssociation<T> collection = namedAssociationFunction.apply( item ); - if( collection == null ) - { - return false; - } - return collection.containsName( name ); - } - - @Override - public String toString() - { - return namedAssociationFunction + " contains name:" + name; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsPredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsPredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsPredicate.java deleted file mode 100644 index e901471..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationContainsPredicate.java +++ /dev/null @@ -1,66 +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.zest.api.query.grammar; - -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.composite.Composite; - -/** - * NamedAssociation Contains Specification. - */ -public class NamedAssociationContainsPredicate<T> - extends ExpressionPredicate -{ - private final NamedAssociationFunction<T> namedAssociationFunction; - private final T value; - - public NamedAssociationContainsPredicate( NamedAssociationFunction<T> namedAssociationFunction, T value ) - { - this.namedAssociationFunction = namedAssociationFunction; - this.value = value; - } - - public NamedAssociationFunction<T> namedAssociation() - { - return namedAssociationFunction; - } - - public T value() - { - return value; - } - - @Override - public boolean test( Composite item ) - { - NamedAssociation<T> collection = namedAssociationFunction.apply( item ); - if( collection == null ) - { - return false; - } - return collection.nameOf( value ) != null; - } - - @Override - public String toString() - { - return namedAssociationFunction + " contains:" + value; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationFunction.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationFunction.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationFunction.java deleted file mode 100644 index 8f0b3de..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/NamedAssociationFunction.java +++ /dev/null @@ -1,118 +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.zest.api.query.grammar; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Member; -import java.lang.reflect.Proxy; -import java.util.function.Function; -import org.apache.zest.api.association.AssociationStateHolder; -import org.apache.zest.api.association.NamedAssociation; -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.composite.CompositeInstance; - -/** - * Function to get Entity NamedAssociations. - */ -public class NamedAssociationFunction<T> - implements Function<Composite, NamedAssociation<T>> -{ - private final AssociationFunction<?> traversedAssociation; - private final ManyAssociationFunction<?> traversedManyAssociation; - private final NamedAssociationFunction<?> traversedNamedAssociation; - private final AccessibleObject accessor; - - public NamedAssociationFunction( AssociationFunction<?> traversedAssociation, - ManyAssociationFunction<?> traversedManyAssociation, - NamedAssociationFunction<?> traversedNamedAssociation, - AccessibleObject accessor - ) - { - this.traversedAssociation = traversedAssociation; - this.traversedManyAssociation = traversedManyAssociation; - this.traversedNamedAssociation = traversedNamedAssociation; - this.accessor = accessor; - } - - public AssociationFunction<?> traversedAssociation() - { - return traversedAssociation; - } - - public ManyAssociationFunction<?> traversedManyAssociation() - { - return traversedManyAssociation; - } - - public NamedAssociationFunction<?> traversedNamedAssociation() - { - return traversedNamedAssociation; - } - - public AccessibleObject accessor() - { - return accessor; - } - - @Override - public NamedAssociation<T> apply( Composite entity ) - { - try - { - Object target = entity; - if( traversedAssociation != null ) - { - target = traversedAssociation.apply( entity ).get(); - } - if( traversedManyAssociation != null ) - { - throw new IllegalArgumentException( "Cannot traverse ManyAssociations" ); - } - if( traversedNamedAssociation != null ) - { - throw new IllegalArgumentException( "Cannot traverse NamedAssociations" ); - } - - CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler( target ); - return ( (AssociationStateHolder) handler.state() ).namedAssociationFor( accessor ); - } - catch( IllegalArgumentException e ) - { - throw e; - } - catch( Throwable e ) - { - throw new IllegalArgumentException( e ); - } - } - - @Override - public String toString() - { - if( traversedAssociation != null ) - { - return traversedAssociation.toString() + "." + ( (Member) accessor ).getName(); - } - else - { - return ( (Member) accessor ).getName(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/NePredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/NePredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/NePredicate.java deleted file mode 100644 index 4e77686..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/NePredicate.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.zest.api.query.grammar; - -/** - * Not equals Specification. - */ -public class NePredicate<T> - extends ComparisonPredicate<T> -{ - public NePredicate( PropertyFunction<T> property, T value ) - { - super( property, value ); - } - - @Override - protected boolean compare( T value ) - { - return !value.equals( this.value ); - } - - @Override - public String toString() - { - return property.toString() + "!=" + value.toString(); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/Notpredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/Notpredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/Notpredicate.java deleted file mode 100644 index 8a2005e..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/Notpredicate.java +++ /dev/null @@ -1,53 +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.zest.api.query.grammar; - -import java.util.function.Predicate; -import org.apache.zest.api.composite.Composite; - -/** - * NOT Specification. - */ -public class Notpredicate implements Predicate<Composite> -{ - private Predicate<Composite> operand; - - public Notpredicate( Predicate<Composite> operand ) - { - this.operand = operand; - } - - public Predicate<Composite> operand() - { - return operand; - } - - @Override - public boolean test( Composite item ) - { - return operand.negate().test( item ); - } - - @Override - public String toString() - { - return "!" + operand.toString(); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/OrPredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/OrPredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/OrPredicate.java deleted file mode 100644 index 70cb7a0..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/OrPredicate.java +++ /dev/null @@ -1,62 +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.zest.api.query.grammar; - -import java.util.Collection; -import java.util.function.Predicate; -import org.apache.zest.api.composite.Composite; - -/** - * OR Specification. - */ -public class OrPredicate - extends BinaryPredicate -{ - - public OrPredicate( Collection<Predicate<Composite>> operands ) - { - super( operands ); - } - - @Override - public boolean test( Composite item ) - { - Predicate<Composite> master = t -> false; - for( Predicate<Composite> p : operands ) - { - master = master.or( p ); - } - return master.test( item ); - } - - @Override - public String toString() - { - StringBuilder sb = new StringBuilder( "(" ); - String or = ""; - for( Predicate<Composite> operand : operands ) - { - sb.append( or ).append( operand ); - or = " or "; - } - return sb.append( ")" ).toString(); - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/OrderBy.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/OrderBy.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/OrderBy.java deleted file mode 100644 index 9426980..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/OrderBy.java +++ /dev/null @@ -1,93 +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.zest.api.query.grammar; - -/** - * Query sorting segment. - */ -public class OrderBy -{ - /** - * Order direction. - */ - public enum Order - { - ASCENDING, DESCENDING - } - - /** - * Order. - */ - private final PropertyFunction<?> propertyReference; - /** - * Direction. - */ - private final Order order; - - /** - * Constructor. - * - * @param propertyReference property that determines the order; cannot be null - * @param order direction - * - * @throws IllegalArgumentException - If property is null - */ - public OrderBy( final PropertyFunction<?> propertyReference, - final Order order - ) - { - if( propertyReference == null ) - { - throw new IllegalArgumentException( "Ordering property cannot be null" ); - } - this.propertyReference = propertyReference; - this.order = order; - } - - /** - * Getter. - * - * @return property; cannot be null - */ - public PropertyFunction<?> property() - { - return propertyReference; - } - - /** - * Getter. - * - * @return direction; cannot be null - */ - public Order order() - { - return order; - } - - @Override - public String toString() - { - return new StringBuilder() - .append( propertyReference ) - .append( " " ) - .append( order ) - .toString(); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyFunction.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyFunction.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyFunction.java deleted file mode 100644 index 800b8ef..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyFunction.java +++ /dev/null @@ -1,180 +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.zest.api.query.grammar; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Member; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Proxy; -import java.lang.reflect.Type; -import java.util.function.Function; -import org.apache.zest.api.association.Association; -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.composite.CompositeInstance; -import org.apache.zest.api.property.GenericPropertyInfo; -import org.apache.zest.api.property.Property; -import org.apache.zest.api.query.NotQueryableException; -import org.apache.zest.api.query.QueryExpressionException; -import org.apache.zest.api.util.Classes; - -import static org.apache.zest.api.util.Classes.typeOf; - -/** - * Function to get Entity Properties. - */ -public class PropertyFunction<T> - implements Function<Composite, Property<T>> -{ - private final PropertyFunction<?> traversedProperty; - private final AssociationFunction<?> traversedAssociation; - private final ManyAssociationFunction<?> traversedManyAssociation; - private final NamedAssociationFunction<?> traversedNamedAssociation; - private final AccessibleObject accessor; - - public PropertyFunction( PropertyFunction<?> traversedProperty, - AssociationFunction<?> traversedAssociation, - ManyAssociationFunction<?> traversedManyAssociation, - NamedAssociationFunction<?> traversedNamedAssociation, - AccessibleObject accessor - ) - { - this.traversedProperty = traversedProperty; - this.traversedAssociation = traversedAssociation; - this.traversedManyAssociation = traversedManyAssociation; - this.traversedNamedAssociation = traversedNamedAssociation; - this.accessor = accessor; - - // Verify that the property accessor is not marked as non queryable - NotQueryableException.throwIfNotQueryable( accessor ); - // Verify that the property type itself (value composites) is not marked as non queryable - - Type returnType = typeOf( accessor ); - if( !Property.class.isAssignableFrom( Classes.RAW_CLASS.apply( returnType ) ) ) - { - throw new QueryExpressionException( "Not a property type:" + returnType ); - } - Type propertyTypeAsType = GenericPropertyInfo.toPropertyType( returnType ); - if( propertyTypeAsType instanceof ParameterizedType ) - { - propertyTypeAsType = ( (ParameterizedType) propertyTypeAsType ).getRawType(); - } - - if( !( propertyTypeAsType instanceof Class ) ) - { - throw new QueryExpressionException( "Unsupported property type:" + propertyTypeAsType ); - } - @SuppressWarnings( "unchecked" ) - Class<T> type = (Class<T>) propertyTypeAsType; - NotQueryableException.throwIfNotQueryable( type ); - } - - public PropertyFunction<?> traversedProperty() - { - return traversedProperty; - } - - public AssociationFunction<?> traversedAssociation() - { - return traversedAssociation; - } - - public ManyAssociationFunction<?> traversedManyAssociation() - { - return traversedManyAssociation; - } - - public NamedAssociationFunction<?> traversedNamedAssociation() - { - return traversedNamedAssociation; - } - - public AccessibleObject accessor() - { - return accessor; - } - - @Override - public Property<T> apply( Composite entity ) - { - try - { - Object target = entity; - if( traversedProperty != null ) - { - Property<?> property = traversedProperty.apply( entity ); - if( property == null ) - { - return null; - } - target = property.get(); - } - else if( traversedAssociation != null ) - { - Association<?> association = traversedAssociation.apply( entity ); - if( association == null ) - { - return null; - } - target = association.get(); - } - else if( traversedManyAssociation != null ) - { - throw new IllegalArgumentException( "Cannot evaluate a ManyAssociation" ); - } - else if( traversedNamedAssociation != null ) - { - throw new IllegalArgumentException( "Cannot evaluate a NamedAssociation" ); - } - - if( target == null ) - { - return null; - } - - CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler( target ); - return handler.state().propertyFor( accessor ); - } - catch( IllegalArgumentException e ) - { - throw e; - } - catch( Throwable e ) - { - throw new IllegalArgumentException( e ); - } - } - - @Override - public String toString() - { - if( traversedProperty != null ) - { - return traversedProperty.toString() + "." + ( (Member) accessor ).getName(); - } - else if( traversedAssociation != null ) - { - return traversedAssociation.toString() + "." + ( (Member) accessor ).getName(); - } - else - { - return ( (Member) accessor ).getName(); - } - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNotNullPredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNotNullPredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNotNullPredicate.java deleted file mode 100644 index bb33cd7..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNotNullPredicate.java +++ /dev/null @@ -1,61 +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.zest.api.query.grammar; - -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.property.Property; - -/** - * Property not null Specification. - */ -public class PropertyNotNullPredicate<T> - extends ExpressionPredicate -{ - private PropertyFunction<T> property; - - public PropertyNotNullPredicate( PropertyFunction<T> property ) - { - this.property = property; - } - - public PropertyFunction<T> property() - { - return property; - } - - @Override - public boolean test( Composite item ) - { - Property<T> prop = property.apply( item ); - - if( prop == null ) - { - return false; - } - - return prop.get() != null; - } - - @Override - public String toString() - { - return property.toString() + "is not null"; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNullPredicate.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNullPredicate.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNullPredicate.java deleted file mode 100644 index c759483..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyNullPredicate.java +++ /dev/null @@ -1,61 +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.zest.api.query.grammar; - -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.property.Property; - -/** - * Property null Specification. - */ -public class PropertyNullPredicate<T> - extends ExpressionPredicate -{ - private PropertyFunction<T> property; - - public PropertyNullPredicate( PropertyFunction<T> property ) - { - this.property = property; - } - - public PropertyFunction<T> property() - { - return property; - } - - @Override - public boolean test( Composite item ) - { - Property<T> prop = property.apply( item ); - - if( prop == null ) - { - return true; - } - - return prop.get() == null; - } - - @Override - public String toString() - { - return property.toString() + "is null"; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyReference.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyReference.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyReference.java deleted file mode 100644 index 69d2861..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/PropertyReference.java +++ /dev/null @@ -1,32 +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.zest.api.query.grammar; - -import java.util.function.Function; -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.property.Property; - -/** - * Property Reference. - */ -public interface PropertyReference -{ - <T> Function<Composite, Property<T>> reference(); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/QuerySpecification.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/QuerySpecification.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/QuerySpecification.java deleted file mode 100644 index c065c43..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/QuerySpecification.java +++ /dev/null @@ -1,72 +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.zest.api.query.grammar; - -import java.util.function.Predicate; -import org.apache.zest.api.composite.Composite; - -/** - * This should be used when doing native queries, such as SQL, SPARQL or similar. EntityFinders can choose - * what type of query languages they can understand by checking the language property of a QuerySpecification - */ -public class QuerySpecification - implements Predicate<Composite> -{ - public static boolean isQueryLanguage( String language, Predicate<Composite> specification ) - { - if( !( specification instanceof QuerySpecification ) ) - { - return false; - } - - return ( (QuerySpecification) specification ).language().equals( language ); - } - - private String language; - private String query; - - public QuerySpecification( String language, String query ) - { - this.language = language; - this.query = query; - } - - public String language() - { - return language; - } - - public String query() - { - return query; - } - - @Override - public boolean test( Composite item ) - { - return false; - } - - @Override - public String toString() - { - return language + ":" + query; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/Variable.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/Variable.java b/core/api/src/main/java/org/apache/zest/api/query/grammar/Variable.java deleted file mode 100644 index 300b04d..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/Variable.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.zest.api.query.grammar; - -/** - * Query Variable name. - */ -public class Variable -{ - private String name; - - public Variable( String name ) - { - this.name = name; - } - - public String variableName() - { - return name; - } - - @Override - public String toString() - { - return name; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/grammar/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/grammar/package.html b/core/api/src/main/java/org/apache/zest/api/query/grammar/package.html deleted file mode 100644 index 4a3e2f1..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/grammar/package.html +++ /dev/null @@ -1,24 +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. - ~ - ~ - --> -<html> - <body> - <h2>Query Grammar.</h2> - </body> -</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/query/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/package.html b/core/api/src/main/java/org/apache/zest/api/query/package.html deleted file mode 100644 index c3ee7ad..0000000 --- a/core/api/src/main/java/org/apache/zest/api/query/package.html +++ /dev/null @@ -1,24 +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. - ~ - ~ - --> -<html> - <body> - <h2>Query API.</h2> - </body> -</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/Availability.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/Availability.java b/core/api/src/main/java/org/apache/zest/api/service/Availability.java deleted file mode 100644 index 9ff18a2..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/Availability.java +++ /dev/null @@ -1,40 +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.zest.api.service; - -/** - * Services can implement this interface in order to allow Polygene to ask - * it whether it is currently available for use or not. This is accessed - * by clients through the ServiceReference of the service. Services that do not - * implement this are always considered to be available. - */ -public interface Availability -{ - /** - * Implementations should return true if the underlying service is currently available for use. - * - * Reasons why a service might not be available is either if it has been configured not to be (see - * the Enabled interface), or if an underlying resource is currently unavailable. - * - * @return true if the service is available, false otherwise. - */ - boolean isAvailable(); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/DuplicateServiceIdentityException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/DuplicateServiceIdentityException.java b/core/api/src/main/java/org/apache/zest/api/service/DuplicateServiceIdentityException.java deleted file mode 100644 index b5b13e6..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/DuplicateServiceIdentityException.java +++ /dev/null @@ -1,35 +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.zest.api.service; - -import org.apache.zest.api.common.InvalidApplicationException; - -/** - * Thrown when a duplicate service reference is detected. - */ -public class DuplicateServiceIdentityException - extends InvalidApplicationException -{ - public DuplicateServiceIdentityException( String string ) - { - super( string ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ImportedServiceDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ImportedServiceDescriptor.java b/core/api/src/main/java/org/apache/zest/api/service/ImportedServiceDescriptor.java deleted file mode 100644 index e68f4b7..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ImportedServiceDescriptor.java +++ /dev/null @@ -1,35 +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.zest.api.service; - -import org.apache.zest.api.composite.ModelDescriptor; -import org.apache.zest.api.identity.Identifiable; - -/** - * {@code ServiceDescriptor} provides meta information of a service. - */ -public interface ImportedServiceDescriptor - extends ModelDescriptor, Identifiable -{ - Class<? extends ServiceImporter> serviceImporter(); - - Class<?> type(); -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/NoSuchServiceException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/NoSuchServiceException.java b/core/api/src/main/java/org/apache/zest/api/service/NoSuchServiceException.java deleted file mode 100644 index a7a3df4..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/NoSuchServiceException.java +++ /dev/null @@ -1,54 +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.zest.api.service; - -import java.util.stream.Collectors; -import org.apache.zest.api.composite.CompositeDescriptor; -import org.apache.zest.api.composite.ModelDescriptor; -import org.apache.zest.api.composite.NoSuchCompositeException; -import org.apache.zest.api.structure.TypeLookup; - -/** - * Thrown when no visible service of the requested type is found. - */ -public class NoSuchServiceException extends NoSuchCompositeException -{ - public NoSuchServiceException( String typeName, String moduleName, TypeLookup typeLookup ) - { - super( "ServiceComposite", typeName, moduleName, formatVisibleTypes( typeLookup ) ); - } - - private static String formatVisibleTypes( TypeLookup typeLookup ) - { - return typeLookup.allServices() - .map( NoSuchServiceException::typeOf ) - .collect( Collectors.joining( "\n", "Visible service types are:\n", "" ) ); - } - - private static String typeOf( ModelDescriptor descriptor ) - { - if( descriptor instanceof CompositeDescriptor ) - { - return ( (CompositeDescriptor) descriptor ).primaryType().getName(); - } - return descriptor.types().map( Class::getName ).collect( Collectors.joining( ",", "[", "]") ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceActivation.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceActivation.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceActivation.java deleted file mode 100644 index 57dbc04..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceActivation.java +++ /dev/null @@ -1,69 +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.zest.api.service; - -import org.apache.zest.api.activation.Activators; - -/** - * Convenience interface for simple Service Activation. - * - * Let your ServiceComposite extends ServiceActivation and implement it in one of its Mixins. - * A corresponding Activator is automatically registered. - */ -@Activators( ServiceActivation.ServiceActivator.class ) -public interface ServiceActivation -{ - - /** - * Called after ServiceComposite Activation. - */ - void activateService() - throws Exception; - - /** - * Called before ServiceComposite Passivation. - */ - void passivateService() - throws Exception; - - /** - * Service Activator. - */ - class ServiceActivator - extends ServiceActivatorAdapter<ServiceActivation> - { - - @Override - public void afterActivation( ServiceReference<ServiceActivation> activated ) - throws Exception - { - activated.get().activateService(); - } - - @Override - public void beforePassivation( ServiceReference<ServiceActivation> passivating ) - throws Exception - { - passivating.get().passivateService(); - } - - } - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceActivatorAdapter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceActivatorAdapter.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceActivatorAdapter.java deleted file mode 100644 index ba780f3..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceActivatorAdapter.java +++ /dev/null @@ -1,71 +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.zest.api.service; - -import org.apache.zest.api.activation.Activator; - -/** - * Adapter for Service Activator. - * - * @param <ServiceType> Type of the service. - */ -public class ServiceActivatorAdapter<ServiceType> - implements Activator<ServiceReference<ServiceType>> -{ - /** - * Called before Service activation. - * @param activating Activating Service - */ - @Override - public void beforeActivation( ServiceReference<ServiceType> activating ) - throws Exception - { - } - - /** - * Called after Service activation. - * @param activated Activated Service - */ - @Override - public void afterActivation( ServiceReference<ServiceType> activated ) - throws Exception - { - } - - /** - * Called before Service passivation. - * @param passivating Passivating Service - */ - @Override - public void beforePassivation( ServiceReference<ServiceType> passivating ) - throws Exception - { - } - - /** - * Called after Service passivation. - * @param passivated Passivated Service - */ - @Override - public void afterPassivation( ServiceReference<ServiceType> passivated ) - throws Exception - { - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceComposite.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceComposite.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceComposite.java deleted file mode 100644 index 48ff167..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceComposite.java +++ /dev/null @@ -1,33 +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.zest.api.service; - -import org.apache.zest.api.composite.Composite; -import org.apache.zest.api.identity.HasIdentity; - -/** - * All Composites being used to implement Services - * must extend this interface. - */ -public interface ServiceComposite - extends HasIdentity, Composite -{ -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceDescriptor.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceDescriptor.java deleted file mode 100644 index 8de5ff1..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceDescriptor.java +++ /dev/null @@ -1,37 +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.zest.api.service; - -import org.apache.zest.api.composite.CompositeDescriptor; -import org.apache.zest.api.composite.StatefulCompositeDescriptor; -import org.apache.zest.api.identity.Identifiable; - -/** - * {@code ServiceDescriptor} provides meta informations of a service. - */ -public interface ServiceDescriptor - extends CompositeDescriptor, StatefulCompositeDescriptor, Identifiable -{ - boolean isInstantiateOnStartup(); - - <T> Class<T> configurationType(); - -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java deleted file mode 100644 index 8c9149f..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceFinder.java +++ /dev/null @@ -1,94 +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.zest.api.service; - -import java.lang.reflect.Type; -import java.util.stream.Stream; - -/** - * Interface used to query for ServiceReferences. - * <p> - * Each ServiceFinder is - * obtained from a specific Module, and the lookup rules are the following: - * </p> - * <ol> - * <li>First look in the same Module as the ServiceFinder</li> - * <li>Then look in the same Layer as the ServiceFinder. Any Services declared - * with Visibility Layer and Application should be included</li> - * <li>Then look in the used Layers. Any Services declared with Visibility Application - * should be included</li> - * </ol> - * <p> - * Both native Polygene services and imported services are considered, with preference to native services. - * </p> - */ -public interface ServiceFinder -{ - /** - * Find a ServiceReference that implements the given type. - * - * @param serviceType the type that the Service must implement - * - * @return a ServiceReference if one is found - * - * @throws NoSuchServiceException if no service of serviceType is found - */ - <T> ServiceReference<T> findService( Class<T> serviceType ) - throws NoSuchServiceException; - - /** - * Find a ServiceReference that implements the given type. - * - * @param serviceType the type that the Service must implement - * - * @return a ServiceReference if one is found - * - * @throws NoSuchServiceException if no service of serviceType is found - */ - <T> ServiceReference<T> findService( Type serviceType ) - throws NoSuchServiceException; - - /** - * Find ServiceReferences that implements the given type. - * <p> - * The order of the references is such that Services more local to the querying - * Module is earlier in the list. - * </p> - * - * @param serviceType the type that the Services must implement - * - * @return a stream of ServiceReferences for the given type. It is empty if none exist - */ - <T> Stream<ServiceReference<T>> findServices( Class<T> serviceType ); - - /** - * Find ServiceReferences that implements the given type. - * <p> - * The order of the references is such that Services more local to the querying - * Module is earlier in the list. - * </p> - * - * @param serviceType the type that the Services must implement - * - * @return a stream of ServiceReferences for the given type. It is empty if none exist - */ - <T> Stream<ServiceReference<T>> findServices( Type serviceType ); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceImporter.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceImporter.java deleted file mode 100644 index 444747d..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceImporter.java +++ /dev/null @@ -1,48 +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.zest.api.service; - -/** - * Import a service from some external source. - */ -public interface ServiceImporter<T> -{ - /** - * Imports an instance of the service type described in the service descriptor. - * - * @param serviceDescriptor The service descriptor. - * - * @return The imported service instance. - * - * @throws ServiceImporterException if import failed. - */ - T importService( ImportedServiceDescriptor serviceDescriptor ) - throws ServiceImporterException; - - /** - * Ask if the service is available or not. - * - * @param instance the instance to be checked - * - * @return true if the service is available, false if not - */ - boolean isAvailable( T instance ); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceImporterException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceImporterException.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceImporterException.java deleted file mode 100644 index bf120e4..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceImporterException.java +++ /dev/null @@ -1,48 +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.zest.api.service; - -/** - * If a ServiceImporter could not import a service - * instance it must throw this exception. - */ -public class ServiceImporterException - extends RuntimeException -{ - public ServiceImporterException() - { - } - - public ServiceImporterException( String string ) - { - super( string ); - } - - public ServiceImporterException( String string, Throwable throwable ) - { - super( string, throwable ); - } - - public ServiceImporterException( Throwable throwable ) - { - super( throwable ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java deleted file mode 100644 index cce52e4..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceReference.java +++ /dev/null @@ -1,55 +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.zest.api.service; - -import org.apache.zest.api.activation.ActivationEventListenerRegistration; -import org.apache.zest.api.composite.ModelDescriptor; -import org.apache.zest.api.identity.Identifiable; -import org.apache.zest.api.structure.MetaInfoHolder; -import org.apache.zest.api.type.HasTypes; - -/** - * From a ServiceReference you can access and modify metadata about a service. - * You can also access the actual service through get(), that can then be invoked. - */ -public interface ServiceReference<T> - extends HasTypes, ActivationEventListenerRegistration, MetaInfoHolder, Identifiable -{ - /** - * @return the actual service - */ - T get(); - - /** - * @return TRUE if the service is active, otherwise return FALSE - */ - boolean isActive(); - - /** - * @return TRUE if the service is available, otherwise return FALSE - */ - boolean isAvailable(); - - /** - * @return the ServiceModel of the service referenced by this ServiceReference. - */ - ModelDescriptor model(); -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/ServiceUnavailableException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/ServiceUnavailableException.java b/core/api/src/main/java/org/apache/zest/api/service/ServiceUnavailableException.java deleted file mode 100644 index 0576339..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/ServiceUnavailableException.java +++ /dev/null @@ -1,37 +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.zest.api.service; - -/** - * Thrown when no available service is found. - */ -public class ServiceUnavailableException - extends RuntimeException -{ - public ServiceUnavailableException( String message ) - { - super( message ); - } - - public ServiceUnavailableException( String message, Throwable cause ) - { - super( message, cause ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/importer/InstanceImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/importer/InstanceImporter.java b/core/api/src/main/java/org/apache/zest/api/service/importer/InstanceImporter.java deleted file mode 100644 index a6ed355..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/importer/InstanceImporter.java +++ /dev/null @@ -1,64 +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.zest.api.service.importer; - -import java.util.Objects; -import java.util.stream.Stream; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.service.ImportedServiceDescriptor; -import org.apache.zest.api.service.ServiceImporter; -import org.apache.zest.api.service.ServiceImporterException; -import org.apache.zest.api.structure.Application; -import org.apache.zest.api.structure.Layer; -import org.apache.zest.api.structure.Module; - -/** - * Return a predefined service instance that was provided as meta-info. Search for meta-info in the following order: - * the service itself, the module of the service, the layer of the service, the whole application. - */ -public final class InstanceImporter<T> - implements ServiceImporter<T> -{ - @Structure - private Application application; - - @Structure - private Layer layer; - - @Structure - private Module module; - - @Override - public T importService( final ImportedServiceDescriptor serviceDescriptor ) - throws ServiceImporterException - { - return Stream.of( serviceDescriptor, module, layer, application ) - .flatMap( holder -> serviceDescriptor.types().map( type -> (T) holder.metaInfo( type ) ) ) - .filter( Objects::nonNull ) - .findFirst().orElse( null ); - } - - @Override - public boolean isAvailable( T instance ) - { - return true; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/importer/NewObjectImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/importer/NewObjectImporter.java b/core/api/src/main/java/org/apache/zest/api/service/importer/NewObjectImporter.java deleted file mode 100644 index 0098dc4..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/importer/NewObjectImporter.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.zest.api.service.importer; - -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.object.ObjectFactory; -import org.apache.zest.api.service.ImportedServiceDescriptor; -import org.apache.zest.api.service.ServiceImporter; -import org.apache.zest.api.service.ServiceImporterException; - -/** - * Import Services using a new registered Object instance. - */ -public final class NewObjectImporter<T> - implements ServiceImporter<T> -{ - @Structure - private ObjectFactory obf; - - @Override - @SuppressWarnings( "unchecked" ) - public T importService( ImportedServiceDescriptor serviceDescriptor ) - throws ServiceImporterException - { - return obf.newObject( (Class<T>) serviceDescriptor.types().findFirst().orElse( null )); - } - - @Override - public boolean isAvailable( T instance ) - { - return true; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java b/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java deleted file mode 100644 index 01302d0..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceInstanceImporter.java +++ /dev/null @@ -1,79 +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.zest.api.service.importer; - -import org.apache.zest.api.identity.Identity; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.service.ImportedServiceDescriptor; -import org.apache.zest.api.service.ServiceFinder; -import org.apache.zest.api.service.ServiceImporter; -import org.apache.zest.api.service.ServiceImporterException; -import org.apache.zest.api.service.ServiceReference; - -/** - * Use a registered service that implements ServiceImporter to do the actual - * import. The service id of the service that this importer should delegate to must - * be set as meta-info on this service. Example: - * <pre><code> - * module.services(MyServiceImporterService.class).identifiedBy("someid"); - * module.importedServices(OtherService.class).importedBy(ServiceInstanceImporter.class).setMetaInfo("someid"); - * </code></pre> - */ -public class ServiceInstanceImporter<T> - implements ServiceImporter<T> -{ - @Structure - ServiceFinder finder; - - ServiceImporter<T> service; - - Identity serviceId; - - @Override - public T importService( ImportedServiceDescriptor importedServiceDescriptor ) - throws ServiceImporterException - { - serviceId = importedServiceDescriptor.metaInfo( Identity.class ); - - return serviceImporter().importService( importedServiceDescriptor ); - } - - @Override - public boolean isAvailable( T instance ) - { - return serviceImporter().isAvailable( instance ); - } - - @SuppressWarnings( {"raw", "unchecked"} ) - private ServiceImporter<T> serviceImporter() - { - if( service == null ) - { - service = finder.findServices( ServiceImporter.class ) - .filter( ref -> ref.identity().equals( serviceId ) ) - .findFirst().map( ServiceReference::get ) - .orElseThrow( () -> new ServiceImporterException( - "No service importer with id '" + serviceId + "' was found" ) - ); - } - return service; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/1c722f44/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java b/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java deleted file mode 100644 index f2d5de6..0000000 --- a/core/api/src/main/java/org/apache/zest/api/service/importer/ServiceSelectorImporter.java +++ /dev/null @@ -1,75 +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.zest.api.service.importer; - -import java.util.function.Predicate; -import java.util.stream.Stream; -import org.apache.zest.api.injection.scope.Structure; -import org.apache.zest.api.service.Availability; -import org.apache.zest.api.service.ImportedServiceDescriptor; -import org.apache.zest.api.service.ServiceFinder; -import org.apache.zest.api.service.ServiceImporter; -import org.apache.zest.api.service.ServiceImporterException; -import org.apache.zest.api.service.ServiceReference; - -/** - * If several services are available with a given type, and you want to constrain - * the current module to use a specific one, then use this importer. Specify a - * Specification<ServiceReference<T>> criteria as meta-info for the service, which will be applied - * to the list of available services, and the first match will be chosen. - * - * This importer will avoid selecting itself, as could be possible if the ServiceQualifier.first() - * filter is used. - */ -public final class ServiceSelectorImporter<T> - implements ServiceImporter<T> -{ - @Structure - private ServiceFinder locator; - - @Override - @SuppressWarnings( { "raw", "unchecked" } ) - public T importService( ImportedServiceDescriptor serviceDescriptor ) - throws ServiceImporterException - { - Predicate<ServiceReference<?>> selector = serviceDescriptor.metaInfo( Predicate.class ); - Class serviceType = serviceDescriptor.types().findFirst().orElse( null ); - - Stream<ServiceReference<T>> services = locator.findServices( serviceType ); - Predicate<ServiceReference<T>> filter = ref -> - { - Predicate selector1 = ref.metaInfo( Predicate.class ); - return selector1 == null || selector == selector1; - }; - return services.filter( filter.and( selector ) ) - .findFirst().map( ServiceReference::get ) - .orElseThrow( - () -> new ServiceImporterException( - "Could not find any service to import that matches the given specification for " - + serviceDescriptor.identity() ) ); - } - - @Override - public boolean isAvailable( T instance ) - { - return !( instance instanceof Availability ) || ( (Availability) instance ).isAvailable(); - } -}
