1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources;
14 import java.lang.reflect.Method;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.PUT;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.Response.Status;
21 import javax.xml.namespace.QName;
23 import org.eclipse.winery.common.ModelUtilities;
24 import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
25 import org.eclipse.winery.model.tosca.TBoolean;
26 import org.eclipse.winery.model.tosca.TEntityType.DerivedFrom;
27 import org.eclipse.winery.repository.backend.BackendUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Models a component instance with name, derived from, abstract, and final <br />
33 * Tags are provided by AbstractComponentInstanceResource
36 * AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinalConfigurationBacked
37 * . We did not include interfaces as the getters are currently only called at
40 public abstract class AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal extends AbstractComponentInstanceResource {
42 private static final Logger logger = LoggerFactory.getLogger(AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.class);
45 protected AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal(TOSCAComponentId id) {
50 * @return The associated name of this resource. CSDPR01 foresees a NCName
51 * name and no ID for an entity type. Therefore, we use the ID as
52 * unique identification and convert it to a name when a read
57 public String getName() {
58 return ModelUtilities.getName(this.getElement());
63 public Response putName(String name) {
64 ModelUtilities.setName(this.getElement(), name);
65 return BackendUtils.persist(this);
70 public String getDerivedFrom() {
71 // TOSCA does not introduce a type like WithNameDerivedFromAbstractFinal
72 // We could enumerate all possible implementing classes
73 // Or use java reflection, what we're doing now.
75 // We have three different "DerivedFrom", for NodeTypeImplementation and RelationshipTypeImplementation, we have to assign to a different "DerivedFrom"
76 // This has to be done in the derived resources
77 DerivedFrom derivedFrom;
79 method = this.getElement().getClass().getMethod("getDerivedFrom");
80 derivedFrom = (DerivedFrom) method.invoke(this.getElement());
81 } catch (ClassCastException e) {
82 AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.logger.error("Seems that *Implementation is now Definitions backed, but not yet fully implented", e);
83 throw new IllegalStateException(e);
84 } catch (Exception e) {
85 AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.logger.error("Could not get derivedFrom", e);
86 throw new IllegalStateException(e);
88 if (derivedFrom == null) {
91 QName typeRef = derivedFrom.getTypeRef();
92 if (typeRef == null) {
95 return typeRef.toString();
101 public Response putDerivedFrom(String type) {
102 QName qname = QName.valueOf(type);
104 // see getDerivedFrom for verbose comments
106 DerivedFrom derivedFrom = new DerivedFrom();
107 derivedFrom.setTypeRef(qname);
109 method = this.getElement().getClass().getMethod("setDerivedFrom", DerivedFrom.class);
110 method.invoke(this.getElement(), derivedFrom);
111 } catch (ClassCastException e) {
112 AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.logger.error("Seems that *Implementation is now Definitions backed, but not yet fully implemented", e);
113 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
114 } catch (Exception e) {
115 AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.logger.error("Could not set derivedFrom", e);
116 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
119 return BackendUtils.persist(this);
123 * @param methodName the method to call: getAbstract|getFinal
124 * @return {@inheritDoc}
126 private String getTBoolean(String methodName) {
127 // see getDerivedFrom for verbose comments
131 method = this.getElement().getClass().getMethod(methodName);
132 tBoolean = (TBoolean) method.invoke(this.getElement());
133 } catch (Exception e) {
134 AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.logger.error("Could not get boolean " + methodName, e);
135 throw new IllegalStateException(e);
137 if (tBoolean == null) {
140 return tBoolean.value();
145 * @param methodName the method to call: setAbstract|setFinal
146 * @return {@inheritDoc}
148 private Response putTBoolean(String tBooleanStr, String methodName) {
149 // see getDerivedFrom for verbose comments
152 TBoolean tBoolean = TBoolean.fromValue(tBooleanStr);
154 method = this.getElement().getClass().getMethod(methodName, TBoolean.class);
155 method.invoke(this.getElement(), tBoolean);
156 } catch (Exception e) {
157 AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal.logger.error("Could not set tBoolean " + methodName, e);
158 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
161 return BackendUtils.persist(this);
165 * Method name is not "getAbstract" as ${it.abstract} does not work as
166 * "abstract" is not allowed at that place
170 public String getIsAbstract() {
171 return this.getTBoolean("getAbstract");
176 public Response putIsAbstract(String isAbstract) {
177 return this.putTBoolean(isAbstract, "setAbstract");
182 public String getIsFinal() {
183 return this.getTBoolean("getFinal");
188 public Response putIsFinal(String isFinal) {
189 return this.putTBoolean(isFinal, "setFinal");
193 * @return resource managing abstract, final, derivedFrom
195 @Path("inheritance/")
196 public InheritanceResource getInheritanceManagement() {
197 return new InheritanceResource(this);