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._support.collections.withoutid;
14 import java.lang.reflect.Constructor;
15 import java.util.List;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.PathParam;
20 import org.eclipse.winery.common.Util;
21 import org.eclipse.winery.repository.Utils;
22 import org.eclipse.winery.repository.resources.AbstractComponentInstanceResource;
23 import org.eclipse.winery.repository.resources._support.IPersistable;
24 import org.eclipse.winery.repository.resources._support.collections.EntityCollectionResource;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.sun.jersey.api.NotFoundException;
31 * Class managing a list of entities. It is intended to manage subresources,
32 * where the TOSCA specification did not specify a unique key. Currently, the
33 * hashCode of the XML String representation is used. If other representation
34 * should be used, the method {@code getEntityResource} has to be overriden.
36 * @param <EntityResourceT> the resource modeling the entity
37 * @param <EntityT> the entity type of single items in the list
39 public abstract class EntityWithoutIdCollectionResource<EntityResourceT extends EntityWithoutIdResource<EntityT>, EntityT> extends EntityCollectionResource<EntityResourceT, EntityT> {
41 private static final Logger logger = LoggerFactory.getLogger(EntityWithoutIdCollectionResource.class);
47 public EntityWithoutIdCollectionResource(Class<EntityResourceT> entityResourceTClazz, Class<EntityT> entityTClazz, List<EntityT> list, IPersistable res) {
48 super(entityResourceTClazz, entityTClazz, list, res);
52 * Method searching the list for an id with the hashcode instead of
57 public EntityResourceT getEntityResource(@PathParam("id") String id) {
58 id = Util.URLdecode(id);
61 idInt = Integer.parseInt(id);
62 } catch (java.lang.NumberFormatException e) {
63 throw new NotFoundException(id + " is not a valid id");
65 EntityT entity = null;
67 for (EntityT c : this.list) {
69 // speed optimization - instead of using getId() we directly use the hash code
70 int hash = Utils.getXMLAsString(c).hashCode();
77 throw new NotFoundException();
79 return this.getEntityResourceInstance(entity, idx);
84 public String getId(EntityT entity) {
85 return IdDeterminationWithHashCode.INSTANCE.getId(entity);
92 protected EntityResourceT getEntityResourceInstance(EntityT entity, int idx) {
93 Constructor<EntityResourceT> constructor;
95 constructor = this.entityResourceTClazz.getConstructor(this.entityTClazz, int.class, List.class, AbstractComponentInstanceResource.class);
96 } catch (Exception e) {
98 constructor = this.entityResourceTClazz.getConstructor(this.entityTClazz, int.class, List.class, IPersistable.class);
99 } catch (NoSuchMethodException | SecurityException e1) {
100 EntityWithoutIdCollectionResource.logger.debug("Could not get constructor", e);
101 throw new IllegalStateException(e);
104 EntityResourceT newInstance;
106 newInstance = constructor.newInstance(entity, idx, this.list, this.res);
107 } catch (Exception e) {
108 EntityWithoutIdCollectionResource.logger.debug("Could not instantiate class", e);
109 throw new IllegalStateException(e);