|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.s60tools.appdep.core.data; |
|
18 |
|
19 import java.io.File; |
|
20 import java.io.IOException; |
|
21 import java.lang.reflect.InvocationTargetException; |
|
22 import java.util.ArrayList; |
|
23 import java.util.Iterator; |
|
24 |
|
25 import org.eclipse.core.runtime.IProgressMonitor; |
|
26 import org.eclipse.jface.operation.IRunnableWithProgress; |
|
27 import org.eclipse.swt.widgets.Display; |
|
28 |
|
29 import com.nokia.s60tools.appdep.core.AppDepCacheIndexManager; |
|
30 import com.nokia.s60tools.appdep.core.ICacheIndexListener; |
|
31 import com.nokia.s60tools.appdep.exceptions.CacheFileDoesNotExistException; |
|
32 import com.nokia.s60tools.appdep.resources.Messages; |
|
33 import com.nokia.s60tools.appdep.ui.wizards.ICacheIndexCreatorObserver; |
|
34 |
|
35 /** |
|
36 * Registers the valid cache index creation requests |
|
37 * and perform creation of registered cache indices |
|
38 * when asked to do so. |
|
39 */ |
|
40 public class CacheIndexCreator implements Runnable, IRunnableWithProgress, ICacheIndexListener{ |
|
41 |
|
42 /** |
|
43 * Observer needing to get informed when cache index creation is ready. |
|
44 */ |
|
45 private final ICacheIndexCreatorObserver indexCreatorObserver; |
|
46 |
|
47 /** |
|
48 * Count of cache indices to be created. |
|
49 * Accessed only via increment and decrement methods |
|
50 */ |
|
51 private int cacheIndicesToBeCreatedCount = 0; |
|
52 |
|
53 /** |
|
54 * Stores cache index creation requests. |
|
55 */ |
|
56 private ArrayList<CacheIndexCreationRequest> cacheIndexCreationReqArr = null; |
|
57 |
|
58 /** |
|
59 * Reference to cache index manager. |
|
60 */ |
|
61 AppDepCacheIndexManager indexMgr = null; |
|
62 |
|
63 /** |
|
64 * Reference to progress monitor of the job launching cache index creation. |
|
65 */ |
|
66 private IProgressMonitor monitorStored = null; |
|
67 |
|
68 /** |
|
69 * Constructor to be launched from JUnit tests. |
|
70 * @param indexCreatorObserver Observer needing to get informed when cache index creation is ready. |
|
71 */ |
|
72 public CacheIndexCreator(){ |
|
73 this.indexCreatorObserver = null; |
|
74 this.allocateMembers(); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Constructor to be launched from UI |
|
79 * @param indexCreatorObserver Observer needing to get informed when cache index creation is ready. |
|
80 */ |
|
81 public CacheIndexCreator(ICacheIndexCreatorObserver indexCreatorObserver){ |
|
82 this.indexCreatorObserver = indexCreatorObserver; |
|
83 allocateMembers(); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Allocates member variables not initialized via constructor parameters. |
|
88 */ |
|
89 public void allocateMembers(){ |
|
90 cacheIndexCreationReqArr = new ArrayList<CacheIndexCreationRequest>(); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Increments cache index creation count. |
|
95 * @return incremented count. |
|
96 */ |
|
97 synchronized private int incrementIndicesToBeCreatedCount(){ |
|
98 return (++cacheIndicesToBeCreatedCount); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Decrements cache index creation count. |
|
103 * @return decremented count. |
|
104 */ |
|
105 synchronized private int decrementIndicesToBeCreatedCount(){ |
|
106 return (--cacheIndicesToBeCreatedCount); |
|
107 } |
|
108 |
|
109 /* (non-Javadoc) |
|
110 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor) |
|
111 */ |
|
112 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
|
113 |
|
114 if(monitorStored == null){ |
|
115 monitorStored = monitor; |
|
116 int steps = cacheIndexCreationReqArr.size(); |
|
117 if(monitorStored != null){ // There is no monitor initialized in JUnit tests |
|
118 monitorStored.beginTask(Messages.getString("CacheIndexCreator.Creating_Cache_Indices"), steps); //$NON-NLS-1$ |
|
119 } |
|
120 handleIndexCreationRequests(); |
|
121 }//if |
|
122 |
|
123 } //run |
|
124 |
|
125 /** |
|
126 * Handles registered cache index creation request. |
|
127 */ |
|
128 private void handleIndexCreationRequests() { |
|
129 for (Iterator<CacheIndexCreationRequest> iter = cacheIndexCreationReqArr.iterator(); iter.hasNext();) { |
|
130 CacheIndexCreationRequest req = iter.next(); |
|
131 try { |
|
132 // Requesting |
|
133 File cacheFile = new File(req.getCacheFile()); |
|
134 if(!cacheFile.exists()){ |
|
135 throw new CacheFileDoesNotExistException(cacheFile.getAbsolutePath()); |
|
136 } |
|
137 CacheIndex.getCacheIndexInstance(cacheFile, req.getBuildDir()); |
|
138 } catch (CacheFileDoesNotExistException e) { |
|
139 e.printStackTrace(); |
|
140 } catch (IOException e) { |
|
141 e.printStackTrace(); |
|
142 } |
|
143 |
|
144 } //for |
|
145 |
|
146 // Resetting array after all request have been handled |
|
147 cacheIndexCreationReqArr.clear(); |
|
148 } |
|
149 |
|
150 /** |
|
151 * Registers cache index creation request for a target identified |
|
152 * in parameters, if does not registered already. |
|
153 * @param cacheFileAbsolutePathName Cache file path name for target to be registerd. |
|
154 * @param buildDirAbsolutePathName Build directory file path name for target to be registerd. |
|
155 */ |
|
156 public void registerCacheIndexCreation(String cacheFileAbsolutePathName, |
|
157 String buildDirAbsolutePathName) { |
|
158 |
|
159 //Checking if we already have the cache index |
|
160 if(CacheIndex.cacheIndexExistsFor(cacheFileAbsolutePathName)){ |
|
161 // Already exists, no need to re-create |
|
162 return; |
|
163 } |
|
164 |
|
165 // Checking that we do not already have the same request?... |
|
166 for (Iterator<CacheIndexCreationRequest> iter = cacheIndexCreationReqArr.iterator(); iter.hasNext();) { |
|
167 CacheIndexCreationRequest req = iter.next(); |
|
168 if(req.getCacheFile().equalsIgnoreCase(cacheFileAbsolutePathName)){ |
|
169 // Request already registered! |
|
170 return; |
|
171 } |
|
172 } |
|
173 |
|
174 // Not registered yet, registering... |
|
175 if(indexMgr == null){ |
|
176 indexMgr = AppDepCacheIndexManager.getInstance(); |
|
177 indexMgr.addListener(this); |
|
178 } |
|
179 |
|
180 incrementIndicesToBeCreatedCount(); |
|
181 cacheIndexCreationReqArr.add(new CacheIndexCreationRequest(cacheFileAbsolutePathName, |
|
182 buildDirAbsolutePathName)); |
|
183 |
|
184 } |
|
185 |
|
186 /** |
|
187 * Checks if there are any cache creation requests. |
|
188 * @return <code>true</code> if requests, otherwise <code>false</code>. |
|
189 */ |
|
190 public boolean hasIndexCreationRequests(){ |
|
191 return (cacheIndexCreationReqArr.size() > 0); |
|
192 } |
|
193 |
|
194 /* (non-Javadoc) |
|
195 * @see com.nokia.s60tools.appdep.core.ICacheIndexListener#cacheIndexCreationCompleted(com.nokia.s60tools.appdep.core.data.CacheIndex) |
|
196 */ |
|
197 public void cacheIndexCreationCompleted(CacheIndex cacheIndexObj) { |
|
198 |
|
199 final ICacheIndexListener listenerObj = this; |
|
200 final CacheIndex cacheIndexObjFinal = cacheIndexObj; |
|
201 |
|
202 Runnable monitorStatusUpdateRunnable = new Runnable(){ |
|
203 public void run(){ |
|
204 try { |
|
205 // One cache index creation step completed |
|
206 if(monitorStored != null){ // There is no monitor initialized in JUnit tests |
|
207 monitorStored.worked(1); |
|
208 } |
|
209 |
|
210 try { |
|
211 // Refreshing tree view |
|
212 if(indexCreatorObserver != null){ |
|
213 indexCreatorObserver.cacheIndexCreated(cacheIndexObjFinal); |
|
214 } |
|
215 } catch (Exception e) { |
|
216 // User might have been Canceled the wizard |
|
217 // and widget is disposed and this call fails |
|
218 // Just then ignoring and making sure that |
|
219 // done() is called for monitor. |
|
220 } |
|
221 |
|
222 // Updating count of work that is still pending |
|
223 // and testing if all the work has been done |
|
224 if(decrementIndicesToBeCreatedCount() == 0){ |
|
225 if(monitorStored != null){ // There is no monitor initialized in JUnit tests |
|
226 monitorStored.done(); |
|
227 } |
|
228 indexMgr.removeListener(listenerObj); |
|
229 // Forcing the manager creation next time |
|
230 // if new registrations happen during |
|
231 // the lifetime of this object |
|
232 indexMgr = null; |
|
233 } |
|
234 } catch (Exception e) { |
|
235 e.printStackTrace(); |
|
236 } |
|
237 } |
|
238 }; |
|
239 |
|
240 // Update request done in its own thread |
|
241 // in order not to cause invalid thread access |
|
242 Display.getDefault().asyncExec(monitorStatusUpdateRunnable); |
|
243 |
|
244 } |
|
245 |
|
246 /* (non-Javadoc) |
|
247 * @see java.lang.Runnable#run() |
|
248 */ |
|
249 public void run() { |
|
250 handleIndexCreationRequests(); |
|
251 } |
|
252 |
|
253 } |