1 /*
2 * Copyright (c) 2007, Fraunhofer-Gesellschaft
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * (1) Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the disclaimer at the end.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * (2) Neither the name of Fraunhofer nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * DISCLAIMER
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35 package org.ogf.graap.wsag.server.engine;
36
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Vector;
42
43 import org.ogf.graap.wsag.server.actions.impl.AgreementFactoryAction;
44 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
45
46 /**
47 * The template registry is the central store of factory actions. An {@link AgreementFactoryAction} implements
48 * the required functionality to create templates, negotiate offer, and create agreement. Each factory action
49 * is identified by the agreement template, which is generated by the factory action.
50 *
51 * @see org.ogf.graap.wsag.server.actions.impl.AgreementFactoryAction
52 * @see org.ogf.graap.wsag.server.actions.AbstractGetTemplateAction
53 * @see org.ogf.graap.wsag.server.actions.AbstractNegotiationAction
54 * @see org.ogf.graap.wsag.server.actions.AbstractCreateAgreementAction
55 *
56 * @author Oliver Waeldrich
57 *
58 */
59 public class TemplateRegistry
60 {
61
62 //
63 // Map for quick lookup of the factory action for a template
64 //
65 private Map<TemplateIdentifier, AgreementFactoryAction> registryMap =
66 new HashMap<TemplateIdentifier, AgreementFactoryAction>();
67
68 //
69 // When getting all templates from the registry, we want to preserve
70 // the order of the templates, as they where added. The registry list
71 // does this for us.
72 //
73 private List<AgreementFactoryAction> registryList = new Vector<AgreementFactoryAction>();
74
75 /**
76 * Adds a new factory action to the registry
77 *
78 * @param action
79 * the action to add
80 */
81 public void add( AgreementFactoryAction action )
82 {
83 synchronized ( registryMap )
84 {
85 registryMap.put( new TemplateIdentifier( action.getTemplate() ), action );
86 registryList.add( action );
87 }
88 }
89
90 /**
91 * Finds a template with a given name.
92 *
93 * @param name
94 * the name of the requested template
95 *
96 * @return the agreement template, or <code>null</code> if not found
97 */
98 public AgreementTemplateType findTemplate( String name )
99 {
100 return findTemplate( name, "" );
101 }
102
103 /**
104 * Finds a template with a given name and version.
105 *
106 * @param name
107 * the name of the requested template
108 *
109 * @param version
110 * the version of the requested template
111 *
112 * @return the agreement template, or <code>null</code> if not found
113 */
114 public AgreementTemplateType findTemplate( String name, String version )
115 {
116 AgreementFactoryAction action = registryMap.get( ( new TemplateIdentifier( name, version ) ) );
117 return ( action == null ) ? null : action.getTemplate();
118 }
119
120 /**
121 * Finds the factory action for a template with a given name.
122 *
123 * @param name
124 * the template name of the requested action
125 *
126 * @return the factory, or <code>null</code> if not found
127 */
128 public AgreementFactoryAction findAction( String name )
129 {
130 return findAction( name, "" );
131 }
132
133 /**
134 * Finds the factory action for a template with a given name.
135 *
136 * @param name
137 * the template name of the requested action
138 *
139 * @param version
140 * the template version of the requested action
141 *
142 * @return the factory, or <code>null</code> if a factory with the given name was not found.
143 */
144 public AgreementFactoryAction findAction( String name, String version )
145 {
146 AgreementFactoryAction action = registryMap.get( new TemplateIdentifier( name, version ) );
147 return ( action == null ) ? null : action;
148 }
149
150 /**
151 * Returns the template entries of this registry in the same order in which they were added.
152 *
153 * @return the registered templates
154 */
155 public AgreementTemplateType[] getAllTemplates()
156 {
157 synchronized ( registryMap )
158 {
159 List<AgreementTemplateType> result = new Vector<AgreementTemplateType>();
160
161 Iterator<AgreementFactoryAction> values = registryList.iterator();
162 while ( values.hasNext() )
163 {
164 AgreementFactoryAction action = values.next();
165 result.add( action.getTemplate() );
166 }
167
168 return (AgreementTemplateType[]) result.toArray( new AgreementTemplateType[result.size()] );
169 }
170 }
171
172 /**
173 * Returns a set of templates that support SLA negotiation. Negotiation is supported for each action that
174 * implements a negotiation strategy, e.g. that registers different action than
175 * {@link org.ogf.graap.wsag.server.actions.impl.NegotiationUnsupportedAction}.
176 *
177 * @return the registered negotiable templates
178 *
179 * @see AgreementFactoryAction#isNegotiationSupported()
180 */
181 public AgreementTemplateType[] getNegotiableTemplates()
182 {
183 synchronized ( registryMap )
184 {
185 List<AgreementTemplateType> result = new Vector<AgreementTemplateType>();
186
187 Iterator<AgreementFactoryAction> values = registryList.iterator();
188 while ( values.hasNext() )
189 {
190 AgreementFactoryAction action = values.next();
191 if ( action.isNegotiationSupported() )
192 {
193 result.add( action.getTemplate() );
194 }
195 }
196
197 return (AgreementTemplateType[]) result.toArray( new AgreementTemplateType[result.size()] );
198 }
199 }
200 }