0
|
1 |
/*
|
|
2 |
* Copyright (c) 2006 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 |
|
|
18 |
|
|
19 |
package com.nokia.s60tools.apiquery.shared.searchmethodregistry;
|
|
20 |
|
|
21 |
import java.util.Collection;
|
|
22 |
import java.util.Collections;
|
|
23 |
import java.util.HashMap;
|
|
24 |
import java.util.Map;
|
|
25 |
|
|
26 |
import com.nokia.s60tools.apiquery.shared.resources.Messages;
|
|
27 |
import com.nokia.s60tools.apiquery.shared.searchmethod.ISearchMethodExtension;
|
|
28 |
import com.nokia.s60tools.apiquery.shared.searchmethod.ISearchMethodExtensionInfo;
|
|
29 |
import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
|
|
30 |
import com.nokia.s60tools.util.console.IConsolePrintUtility;
|
|
31 |
import com.nokia.s60tools.util.debug.DbgUtility;
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Singleton class that is created on plugin
|
|
38 |
* startup, and is kept active as long as plugin is active.
|
|
39 |
*
|
|
40 |
* The purpose of this class is to store search method
|
|
41 |
* extensions that were found from the system.
|
|
42 |
*/
|
|
43 |
public class SearchMethodExtensionRegistry {
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Singleton instance.
|
|
47 |
*/
|
|
48 |
static private SearchMethodExtensionRegistry instance = null;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Listeners interested in job completions operations.
|
|
52 |
*/
|
|
53 |
private static Map<String, ISearchMethodExtension> extensions = Collections.synchronizedMap(new HashMap<String, ISearchMethodExtension>());
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Public Singleton instance accessor.
|
|
57 |
* @return Returns instance of this singleton class-
|
|
58 |
*/
|
|
59 |
public static SearchMethodExtensionRegistry getInstance(){
|
|
60 |
if( instance == null ){
|
|
61 |
instance = new SearchMethodExtensionRegistry();
|
|
62 |
}
|
|
63 |
return instance;
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Private default constructor.
|
|
68 |
*/
|
|
69 |
private SearchMethodExtensionRegistry() {
|
|
70 |
DbgUtility.println(DbgUtility.PRIORITY_CLASS, "-- <<create>> --> " + getClass().getName()); //$NON-NLS-1$
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Add a new Search Method
|
|
75 |
* @param extension
|
|
76 |
*/
|
|
77 |
public void addExtension(ISearchMethodExtension extension){
|
|
78 |
synchronized(extensions){
|
|
79 |
ISearchMethodExtensionInfo info = extension.getExtensionInfo();
|
|
80 |
String id = info.getId();
|
|
81 |
ISearchMethodExtension possibleDuplicate = extensions.get(id);
|
|
82 |
if( possibleDuplicate != null ){
|
|
83 |
String errMsg = Messages.getString("SearchMethodExtensionRegistry.Failed_To_Add_Ext_ConsoleMsg") //$NON-NLS-1$
|
|
84 |
+ Messages.getString("SearchMethodExtensionRegistry.Extension_Prefix") //$NON-NLS-1$
|
|
85 |
+ id
|
|
86 |
+ Messages.getString("SearchMethodExtensionRegistry.Is_Registered_Already_MsgPostfix"); //$NON-NLS-1$
|
|
87 |
APIQueryConsole.getInstance().println(errMsg, IConsolePrintUtility.MSG_ERROR);
|
|
88 |
throw new RuntimeException(errMsg);
|
|
89 |
}
|
|
90 |
extensions.put(id, extension);
|
|
91 |
} // synchronized
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Remove a Search Method
|
|
96 |
* @param extension
|
|
97 |
*/
|
|
98 |
public void removeExtension(ISearchMethodExtension extension){
|
|
99 |
synchronized(extensions){
|
|
100 |
ISearchMethodExtensionInfo info = extension.getExtensionInfo();
|
|
101 |
String id = info.getId();
|
|
102 |
ISearchMethodExtension possibleDuplicate = extensions.get(id);
|
|
103 |
if( possibleDuplicate == null ){
|
|
104 |
String errMsg = Messages.getString("SearchMethodExtensionRegistry.Failed_To_Remove_Ext_ConsoleMsg") //$NON-NLS-1$
|
|
105 |
+ Messages.getString("SearchMethodExtensionRegistry.Extension_Prefix") //$NON-NLS-1$
|
|
106 |
+ id
|
|
107 |
+ Messages.getString("SearchMethodExtensionRegistry.Is_Registered_Already_MsgPostfix"); //$NON-NLS-1$
|
|
108 |
APIQueryConsole.getInstance().println(errMsg, IConsolePrintUtility.MSG_ERROR);
|
|
109 |
throw new RuntimeException(errMsg);
|
|
110 |
}
|
|
111 |
extension.notifyExtensionShutdown();
|
|
112 |
extensions.remove(extension);
|
|
113 |
} // synchronized
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Called before host plugin shutdown.
|
|
118 |
*/
|
|
119 |
public void notifyPluginShutdown(){
|
|
120 |
for (ISearchMethodExtension ext : getExtensions()) {
|
|
121 |
ext.notifyExtensionShutdown();
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* Returns the iterator for the currently registered extensions.
|
|
127 |
* @return Returns the currently registered extensions.
|
|
128 |
*/
|
|
129 |
public Collection<ISearchMethodExtension> getExtensions(){
|
|
130 |
return extensions.values();
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Gets the amount of currently registered extensions.
|
|
135 |
* @return Returns the amount of currently registered extensions.
|
|
136 |
*/
|
|
137 |
public int getExtensionCount(){
|
|
138 |
return getExtensions().size();
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Gets the extension that matches with the given description.
|
|
143 |
* @param extensionDescription Description of the search method.
|
|
144 |
* @return The extension matching the description or <code>null</code> if not found.
|
|
145 |
*/
|
|
146 |
public ISearchMethodExtension getByDescription(String extensionDescription) {
|
|
147 |
for (ISearchMethodExtension ext : getExtensions()) {
|
|
148 |
ISearchMethodExtensionInfo extInfo = ext.getExtensionInfo();
|
|
149 |
if(extInfo.getDescription().equals(extensionDescription)){
|
|
150 |
return ext;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
return null;
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Gets the extension that matches with the given id.
|
|
158 |
* @param id Id of the search method.
|
|
159 |
* @return The extension matching the id or <code>null</code> if not found.
|
|
160 |
*/
|
|
161 |
public ISearchMethodExtension getById(String id) {
|
|
162 |
for (ISearchMethodExtension ext : getExtensions()) {
|
|
163 |
ISearchMethodExtensionInfo extInfo = ext.getExtensionInfo();
|
|
164 |
if(extInfo.getId().equals(id)){
|
|
165 |
return ext;
|
|
166 |
}
|
|
167 |
}
|
|
168 |
return null;
|
|
169 |
}
|
|
170 |
}
|