Source code: com/flexstor/flexdbserver/services/ServiceQueue.java
1 /*
2 * ServiceQueue.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:38 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.flexdbserver.services;
12
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.util.StringTokenizer;
16 import java.util.Vector;
17
18 import com.flexstor.common.services.ServiceArgumentsI;
19 import com.flexstor.common.util.Diagnostic;
20 import com.flexstor.common.util.PropertyMapper;
21 import com.flexstor.common.util.PropertyMapperException;
22
23 public class ServiceQueue
24 {
25 private Hashtable table = null;
26 private Hashtable sync_services = null;
27
28
29 public ServiceQueue()
30 {
31 table = new Hashtable();
32 }
33
34 public synchronized void addItem(ServiceQueueItem assetMgr, String name)
35 {
36 Vector serviceQ = null;
37
38 // Get a vector which holds items for the specific service
39 name = findServiceRelationship( name );
40 System.out.println(" #### Adding " + name + " to the service queue. #### ");
41 serviceQ = (Vector)table.get(name);
42 // if Vector does not yet exists, create it
43 if(serviceQ == null)
44 {
45 serviceQ = new Vector();
46 table.put(name, serviceQ);
47 Diagnostic.trace(Diagnostic.APPSERVER_SERVICES, "Creating new Q for " + name);
48 }
49 Diagnostic.trace(Diagnostic.APPSERVER_SERVICES, "Adding element to " + name);
50 serviceQ.addElement(assetMgr);
51
52 }
53
54 public synchronized ServiceQueueItem checkQueue(String servicename)
55 {
56 Vector serviceQ = null;
57 ServiceQueueItem nextItem = null;
58
59 servicename = findServiceRelationship( servicename );
60 System.out.println(" #### Retrieving " + servicename + " from the service queue. ####");
61 serviceQ = (Vector)table.get(servicename);
62 if(serviceQ == null || serviceQ.size() == 0)
63 {
64 Diagnostic.trace(Diagnostic.APPSERVER_SERVICES, "Currently 0 items are waiting");
65 return null;
66 }
67 else
68 {
69 Diagnostic.trace(Diagnostic.APPSERVER_SERVICES, "Currently " + serviceQ.size() + " items are waiting");
70 nextItem = (ServiceQueueItem)serviceQ.elementAt(0);
71 serviceQ.removeElementAt(0);
72 // If a failure occured in the manager, it will no longer be waiting
73 // for a service Object.
74 if(!nextItem.isWaiting())
75 nextItem = null;
76 }
77 return nextItem;
78 }
79
80 /**
81 * Scan services in services.config looking for any synchronize_with property
82 * for creating relationship between services.
83 * A relationship means that a service cannot be executed until another service in the
84 * relationship is done.
85 * The synchronize_with property will also ensure that the max_intances = 1
86 *
87 * @see com.flexstor.common.services.AssetService
88 */
89 public synchronized void setServicesSynchronization( PropertyMapper serviceMap )
90 {
91 // In this method we will attempt to create a hasthable containing the relationship among
92 // synchronized services.
93 // The key will be the identifier for the relationship ( SYNC_x ); this key will be the
94 // the actual servicename added in the queue. The value will be a vector containing a list
95 // of servicenames which belong to this relationship.
96 String[] services = serviceMap.getSections();
97 String service = null;
98 String synchronize_with = null;
99 StringTokenizer tokenizer = null;
100 Vector sync_list = null;
101 int list_counter = 1;
102 boolean bListFound = false;
103
104 if ( services.length > 0 )
105 {
106 for ( int i = 0; i < services.length; i++ )
107 {
108 try
109 {
110 synchronize_with = serviceMap.getValue( services[i], ServiceArgumentsI.SYNCHRONIZE_WITH );
111 tokenizer = new StringTokenizer( synchronize_with, " ," );
112 while ( tokenizer.hasMoreTokens() )
113 {
114 service = tokenizer.nextToken();
115 if ( sync_services == null )
116 {
117 sync_services = new Hashtable();
118 sync_list = new Vector();
119 sync_list.addElement( services[i] );
120 sync_list.addElement( service );
121 sync_services.put( "SYNC_" + list_counter++, sync_list );
122 }
123 else
124 {
125 for ( Enumeration e = sync_services.keys(); e.hasMoreElements(); )
126 {
127 sync_list = (Vector) sync_services.get( (String) e.nextElement() );
128 if ( sync_list.contains( service ) )
129 {
130 if ( sync_list.contains( services[i] ) == false )
131 sync_list.addElement( services[i] );
132 bListFound = true;
133 break;
134 }
135 else if ( sync_list.contains( services[i] ) )
136 {
137 sync_list.addElement( service );
138 bListFound = true;
139 break;
140 }
141 }
142 if ( !bListFound )
143 {
144 sync_list = new Vector();
145 sync_list.addElement( service );
146 sync_list.addElement( services[i] );
147 sync_services.put( "SYNC_" + list_counter++, sync_list );
148 }
149 else
150 bListFound = false; // Set to false for next iteraction
151 }
152 }
153 }
154 catch( PropertyMapperException rpme ) {}
155 }
156 }
157 if(sync_services != null)
158 System.out.println(" ##### The Hashtable for sync services #### \r\n" + sync_services.toString() );
159 else
160 System.out.println(" ##### The Hashtable for sync services could not be created #####" );
161 }
162
163 /**
164 * When adding or checking the queue for a service; first we will see if the service is in a
165 * relationship with other services; we will do that by searching the sync_services hashtable
166 * and getting the identifier for the relation; if a identifier is not found; then the servicename
167 * is the actual element in the queue.
168 */
169 public String findServiceRelationship( String servicename )
170 {
171 if ( sync_services == null || sync_services.size() == 0 )
172 return servicename;
173
174 String sync_identifier = null;
175 Vector sync_list = null;
176
177 for ( Enumeration e = sync_services.keys(); e.hasMoreElements(); )
178 {
179 sync_identifier = (String) e.nextElement();
180 sync_list = (Vector) sync_services.get( sync_identifier );
181 if ( sync_list.contains( servicename ) )
182 return sync_identifier;
183 }
184 return servicename;
185 }
186
187 public boolean isServiceSynchronized( String servicename )
188 {
189 String identifier = findServiceRelationship( servicename );
190 if ( identifier.equals( servicename ) )
191 return false;
192 else
193 return true;
194 }
195 }