This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.models.api-1.2.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-api.git
commit 765c7f8d1ecaca62295a143c584ce2b099674d7f Author: Justin Edelson <[email protected]> AuthorDate: Wed Oct 1 20:04:24 2014 +0000 SLING-3709 - introduce new ModelFactory service interface which throws various exceptions based on failure conditions. Thanks to Konrad Windszus for the original patch! git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api@1628824 13f79535-47bb-0310-9956-ffa450edef68 --- .../models/factory/InvalidAdaptableException.java | 34 ++++++++++++ .../models/factory/InvalidModelException.java | 35 +++++++++++++ .../models/factory/MissingElementsException.java | 61 ++++++++++++++++++++++ .../apache/sling/models/factory/ModelFactory.java | 56 ++++++++++++++++++++ .../apache/sling/models/factory/package-info.java | 20 +++++++ 5 files changed, 206 insertions(+) diff --git a/src/main/java/org/apache/sling/models/factory/InvalidAdaptableException.java b/src/main/java/org/apache/sling/models/factory/InvalidAdaptableException.java new file mode 100644 index 0000000..03485d4 --- /dev/null +++ b/src/main/java/org/apache/sling/models/factory/InvalidAdaptableException.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.models.factory; + +/** + * Exception which is triggered whenever a Sling Model could not be + * instantiated because it could not be adapted from the given adaptable. + * + * @see ModelFactory + * + */ +public class InvalidAdaptableException extends RuntimeException { + private static final long serialVersionUID = -1209301268928038702L; + + public InvalidAdaptableException(String message) { + super(message); + } +} diff --git a/src/main/java/org/apache/sling/models/factory/InvalidModelException.java b/src/main/java/org/apache/sling/models/factory/InvalidModelException.java new file mode 100644 index 0000000..b0e8ae0 --- /dev/null +++ b/src/main/java/org/apache/sling/models/factory/InvalidModelException.java @@ -0,0 +1,35 @@ +/* + * 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.sling.models.factory; + +/** + * Exception which is triggered when the Model could not be instantiated due to + * model class is not having a model annotation, some reflection error, invalid constructors or + * some exception within the post construct method was triggered. + * + * @see ModelFactory + */ +public class InvalidModelException extends RuntimeException { + + private static final long serialVersionUID = 4323592065808565135L; + + public InvalidModelException(String message) { + super(message); + } +} diff --git a/src/main/java/org/apache/sling/models/factory/MissingElementsException.java b/src/main/java/org/apache/sling/models/factory/MissingElementsException.java new file mode 100644 index 0000000..cec85ea --- /dev/null +++ b/src/main/java/org/apache/sling/models/factory/MissingElementsException.java @@ -0,0 +1,61 @@ +/* + * 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.sling.models.factory; + +import java.lang.reflect.AnnotatedElement; +import java.util.Collection; + + +/** + * Exception which is triggered whenever a Sling Model cannot be instantiated + * due to some missing elements (i.e. required fields/methods/constructor params + * could not be injected). + * + * @see ModelFactory + * + */ +public class MissingElementsException extends RuntimeException { + private static final long serialVersionUID = 7870762030809272254L; + + private final Collection<? extends AnnotatedElement> missingElements; + + private String formatString; + + private Class<?> type; + + public MissingElementsException(String format, Collection<? extends AnnotatedElement> elements, Class<?> type) { + super(); + this.formatString = format; + this.missingElements = elements; + this.type = type; + } + + @Override + public String getMessage() { + return String.format(formatString, missingElements, type); + } + + public Class<?> getType() { + return type; + } + + public Collection<? extends AnnotatedElement> getMissingElements() { + return missingElements; + } +} diff --git a/src/main/java/org/apache/sling/models/factory/ModelFactory.java b/src/main/java/org/apache/sling/models/factory/ModelFactory.java new file mode 100644 index 0000000..69cf4da --- /dev/null +++ b/src/main/java/org/apache/sling/models/factory/ModelFactory.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.models.factory; + +/** + * The ModelFactory instantiates Sling Model classes similar to adaptTo but is allowed to throw an exception in case + * instantiation fails for some reason. + * + */ +public interface ModelFactory { + /** + * Instantiates the given Sling Model class from the given adaptable + * @param adaptable the adaptable to use to instantiate the Sling Model Class + * @param type the class to instantiate + * @return a new instance for the required model (never null) + * @throws MissingElementsException in case no injector was able to inject some required values with the given types + * @throws InvalidAdaptableException in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation) + * @throws InvalidModelException in case the model could not be instanciated because model annotation was missing, reflection failed, no valid constructor was found or post-construct has thrown an error + */ + public <ModelType> ModelType createModel(Object adaptable, Class<ModelType> type) throws MissingElementsException, + InvalidAdaptableException, InvalidModelException; + + /** + * + * @param modelClass the class to check + * @param adaptable the adaptable to check + * @return false in case the given class can not be adapted from the given adaptable + * @throws InvalidModelException in case the given class does not have a model annotation + */ + public boolean canCreateFromAdaptable(Class<?> modelClass, Object adaptable) throws InvalidModelException; + + /** + * + * @param modelClass the class to check + * @return false in case the given class has no model annotation + * + * @see org.apache.sling.models.annotations.Model + */ + public boolean isModelClass(Class<?> modelClass); +} diff --git a/src/main/java/org/apache/sling/models/factory/package-info.java b/src/main/java/org/apache/sling/models/factory/package-info.java new file mode 100644 index 0000000..334ad12 --- /dev/null +++ b/src/main/java/org/apache/sling/models/factory/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ +@Version("1.0.0") +package org.apache.sling.models.factory; + +import aQute.bnd.annotation.Version; \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
