View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: PersistenceProviderLocator.java
13   * Created: 2009-07-06
14   * Author: tmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.data.persistence;
19  
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.eclipse.core.runtime.CoreException;
25  import org.eclipse.core.runtime.IConfigurationElement;
26  import org.eclipse.core.runtime.Platform;
27  
28  import pl.edu.agh.cast.Activator;
29  import pl.edu.agh.cast.data.persistence.serialize.SerializationPersistenceProviderFactory;
30  
31  /**
32   * Locator of {@link TransparentPersistenceProvider} instance.
33   *
34   * @author AGH CAST Team
35   */
36  @SuppressWarnings("nls")
37  public final class PersistenceProviderLocator {
38  
39  	private static Logger log = Activator.getLogger();
40  
41  	// BEGIN Persistence Provider Extension Point Specification
42  
43  	/**
44  	 * Name of persistence provider extension point id.
45  	 */
46  	private static final String PERSISTENCE_PROVIDER_EXTENSION_ID = "pl.edu.agh.cast.persistence.provider";
47  
48  	/**
49  	 * Name of attribute holding persistence provider factory class.
50  	 */
51  	private static final String PERSISTENCE_PROVIDER_FACTORY = "factory";
52  
53  	/**
54  	 * Name of attribute holding persistence provider ID.
55  	 */
56  	private static final String PERSISTENCE_PROVIDER_ID = "id";
57  
58  	/**
59  	 * Name of attribute holding persistence provider name.
60  	 */
61  	private static final String PERSISTENCE_PROVIDER_NAME = "name";
62  
63  	/**
64  	 * Name of attribute holding persistence provider description.
65  	 */
66  	private static final String PERSISTENCE_PROVIDER_DESCRIPTION = "description";
67  
68  	/**
69  	 * Persistence provider specification holder.
70  	 *
71  	 * @author AGH CAST Team
72  	 */
73  	private static final class PersistenceProviderSpecification {
74  
75  		private String id;
76  
77  		private String name;
78  
79  		private String description;
80  
81  		public final String getId() {
82  			return id;
83  		}
84  
85  		public final void setId(String id) {
86  			this.id = id;
87  		}
88  
89  		public final String getName() {
90  			return name;
91  		}
92  
93  		public final void setName(String name) {
94  			this.name = name;
95  		}
96  
97  		public final String getDescription() {
98  			return description;
99  		}
100 
101 		public final void setDescription(String description) {
102 			this.description = description;
103 		}
104 
105 	}
106 
107 	// END Persistence Provider Extension Point Specification
108 
109 	/**
110 	 * Persistence provider instance holder.
111 	 *
112 	 * @author AGH CAST Team
113 	 */
114 	private static final class PersistenceProviderHolder {
115 		private static final TransparentPersistenceProvider INSTANCE = createProvider();
116 	}
117 
118 	/**
119 	 * Returns the instance of persistence provider.
120 	 *
121 	 * @return the instance of persistence provider
122 	 */
123 	public static TransparentPersistenceProvider getProvider() {
124 		return PersistenceProviderHolder.INSTANCE;
125 	}
126 
127 	private static TransparentPersistenceProvider createProvider() {
128 		return new TransparentPersistenceProvider();
129 	}
130 
131 	/**
132 	 * Attempts to create an instance of persistence provider with given ID using the specified persistence file.
133 	 *
134 	 * @param providerId
135 	 *            the ID of the persistence provider
136 	 * @param filePath
137 	 *            the path to persistence file
138 	 * @return a new instance of persistence provider
139 	 *
140 	 * @throws PersistenceException
141 	 *             if provider with given ID could not be created
142 	 */
143 	public static IPersistenceProvider createPersistenceProvider(String providerId, String filePath)
144 	        throws PersistenceException {
145 		IPersistenceProviderFactory factory = getPersistenceProviderFactory(providerId);
146 		if (factory == null) {
147 			throw new PersistenceException(String.format("Failed to initialize persistence provider with ID: %s",
148 			        providerId));
149 		}
150 		return factory.createPersistenceProvider(filePath);
151 	}
152 
153 	/**
154 	 * Selects a persistence provider to be used in a new project.
155 	 *
156 	 * @return the ID of selected persistence provider
157 	 */
158 	public static String selectPersistenceProvider() {
159 		return SerializationPersistenceProviderFactory.PROVIDER_ID;
160 	}
161 
162 	/**
163 	 * Returns the path to persistence file for provider with given ID.
164 	 *
165 	 * @param providerId
166 	 *            the ID of the persistence provider
167 	 * @return the path to the persistence file (relative to the project directory)
168 	 */
169 	public static String getPersistenceFilePath(String providerId) {
170 		IPersistenceProviderFactory factory = getPersistenceProviderFactory(providerId);
171 		if (factory != null) {
172 			return factory.getPersistenceFilePath();
173 		}
174 		return String.format("%s.data", providerId);
175 	}
176 
177 	// BEGIN Extension Helper Methods
178 
179 	private static List<PersistenceProviderSpecification> getPersistenceProviderSpecifications() {
180 		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
181 		        PERSISTENCE_PROVIDER_EXTENSION_ID);
182 		List<PersistenceProviderSpecification> specs = new LinkedList<PersistenceProviderSpecification>();
183 		for (IConfigurationElement element : elements) {
184 			PersistenceProviderSpecification spec = new PersistenceProviderSpecification();
185 			spec.setId(element.getAttribute(PERSISTENCE_PROVIDER_ID));
186 			spec.setName(element.getAttribute(PERSISTENCE_PROVIDER_NAME));
187 			spec.setDescription(element.getAttribute(PERSISTENCE_PROVIDER_DESCRIPTION));
188 			specs.add(spec);
189 		}
190 		return specs;
191 	}
192 
193 	private static IPersistenceProviderFactory getPersistenceProviderFactory(String providerId) {
194 		if (providerId == null) {
195 			throw new IllegalArgumentException("Provider ID cannot be null"); //$NON-NLS-1$
196 		}
197 
198 		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
199 		        PERSISTENCE_PROVIDER_EXTENSION_ID);
200 
201 		for (IConfigurationElement element : elements) {
202 			if (providerId.equals(element.getAttribute(PERSISTENCE_PROVIDER_ID))) {
203 				try {
204 					return (IPersistenceProviderFactory)element.createExecutableExtension(PERSISTENCE_PROVIDER_FACTORY);
205 				} catch (CoreException e) {
206 					log.error(String.format(
207 					        "Failed to instantiate persistence provider factory provided by extension: %1$s ", element
208 					                .getDeclaringExtension().getUniqueIdentifier()), e);
209 					return null;
210 				}
211 			}
212 		}
213 
214 		log.error(String.format("Failed to locate persistence provider factory with given ID: %1$s", providerId));
215 
216 		return null;
217 	}
218 
219 	// END Extension Helper Methods
220 
221 }