Skip to content

Package: CDOProvider

CDOProvider

nameinstructionbranchcomplexitylinemethod
CDOProvider()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
cloneProject(InternalProject, InternalProject)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createLocalStore(InternalProject, File)
M: 43 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
createProject(InternalProject)
M: 52 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
createWorkspaceBase(InternalProject, File)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
createWorkspaceConfiguration(InternalProject)
M: 63 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
disposeProject(InternalProject)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
doDelete(InternalProject, Collection)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
doDispose()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
fillChildren(ECPContainer, Object, InternalChildrenList)
M: 95 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
getAdapter(Object, Class)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getElements(InternalProject)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getInstance()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getModelContext(Object)
M: 33 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getProjectData(InternalProject)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getProjectFolder(InternalProject)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getRepositoryData(InternalRepository)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getRoot(InternalProject)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
handleLifecycle(ECPContainer, InternalProvider.LifecycleEvent)
M: 29 C: 0
0%
M: 7 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
isSlow(Object)
M: 14 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
isThreadSafe()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
removeProject(InternalProject)
M: 12 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011 Eike Stepper (Berlin, Germany) and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eike Stepper - initial API and implementation
13: *******************************************************************************/
14: package org.eclipse.emf.ecp.cdo.internal.core;
15:
16: import java.io.File;
17: import java.io.FileNotFoundException;
18: import java.io.PrintStream;
19: import java.util.Collection;
20: import java.util.Set;
21:
22: import org.eclipse.emf.cdo.common.branch.CDOBranch;
23: import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
24: import org.eclipse.emf.cdo.eresource.CDOResource;
25: import org.eclipse.emf.cdo.net4j.CDONet4jSession;
26: import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
27: import org.eclipse.emf.cdo.server.db.CDODBUtil;
28: import org.eclipse.emf.cdo.server.db.IDBStore;
29: import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
30: import org.eclipse.emf.cdo.session.CDOSessionConfigurationFactory;
31: import org.eclipse.emf.cdo.util.CDOUtil;
32: import org.eclipse.emf.cdo.workspace.CDOWorkspaceBase;
33: import org.eclipse.emf.cdo.workspace.CDOWorkspaceConfiguration;
34: import org.eclipse.emf.cdo.workspace.CDOWorkspaceUtil;
35: import org.eclipse.emf.common.notify.Notifier;
36: import org.eclipse.emf.common.util.EList;
37: import org.eclipse.emf.ecore.EObject;
38: import org.eclipse.emf.ecp.core.util.ECPContainer;
39: import org.eclipse.emf.ecp.core.util.ECPProperties;
40: import org.eclipse.emf.ecp.spi.core.DefaultProvider;
41: import org.eclipse.emf.ecp.spi.core.InternalProject;
42: import org.eclipse.emf.ecp.spi.core.InternalRepository;
43: import org.eclipse.emf.ecp.spi.core.util.InternalChildrenList;
44: import org.eclipse.net4j.db.DBUtil;
45: import org.eclipse.net4j.db.IDBAdapter;
46: import org.eclipse.net4j.db.IDBConnectionProvider;
47: import org.eclipse.net4j.db.h2.H2Adapter;
48: import org.eclipse.net4j.util.UUIDGenerator;
49: import org.eclipse.net4j.util.io.IOUtil;
50: import org.h2.jdbcx.JdbcDataSource;
51:
52: /**
53: * @author Eike Stepper
54: */
55: public class CDOProvider extends DefaultProvider {
56:         /**
57:          * The unique provider name.
58:          */
59:         public static final String NAME = "org.eclipse.emf.ecp.cdo.provider"; //$NON-NLS-1$
60:
61:         /**
62:          * The key for the connector type ECP project property.
63:          */
64:         public static final String PROP_CONNECTOR_TYPE = "connectorType"; //$NON-NLS-1$
65:
66:         /**
67:          * The key for the connector description ECP project property.
68:          */
69:         public static final String PROP_CONNECTOR_DESCRIPTION = "connectorDescription"; //$NON-NLS-1$
70:
71:         /**
72:          * The key for the repository name ECP project property.
73:          */
74:         public static final String PROP_REPOSITORY_NAME = "repositoryName"; //$NON-NLS-1$
75:
76:         /**
77:          * The key for the branch path ECP project property.
78:          */
79:         public static final String PROP_BRANCH_PATH = "branchPath"; //$NON-NLS-1$
80:
81:         /**
82:          * The key for the time stamp ECP project property.
83:          */
84:         public static final String PROP_TIME_STAMP = "timeStamp"; //$NON-NLS-1$
85:
86:         /**
87:          * The key for the workspace ID ECP project property.
88:          */
89:         public static final String PROP_WORKSPACE_ID = "workspaceID"; //$NON-NLS-1$
90:
91:         /**
92:          * Contains the singleton instance of this provider.
93:          */
94:         private static CDOProvider instance;
95:
96:         /**
97:          * Default constructor.
98:          */
99:         @SuppressWarnings("deprecation")
100:         public CDOProvider() {
101:                 super(NAME);
102:                 instance = this;
103:                 CDOUtil.setLegacyModeDefault(true);
104:         }
105:
106:         /**
107:          * Get the CDO Provider singleton.
108:          *
109:          * @return the singleton instance or null
110:          * @deprecated use ECPUtil.getECPProviderRegistry().getProvider(CDOProvider.NAME) instead
111:          */
112:         @Deprecated
113:         public static CDOProvider getInstance() {
114:                 // TODO: what if instance is still null because constructor was never called?
115:                 return instance;
116:         }
117:
118:         @Override
119:         protected void doDispose() {
120:                 instance = null;
121:                 super.doDispose();
122:         }
123:
124:         @Override
125:         public <T> T getAdapter(Object adaptable, Class<T> adapterType) {
126:                 final T adapter = ECPProjectAdapterFactory.adapt(adaptable, adapterType);
127:•                if (adapter != null) {
128:                         return adapter;
129:                 }
130:
131:                 return super.getAdapter(adaptable, adapterType);
132:         }
133:
134:         @Override
135:         public boolean isSlow(Object parent) {
136:•                if (parent instanceof CDOBranchWrapper) {
137:                         return true;
138:                 }
139:
140:•                if (parent instanceof EObject) {
141:                         return true;
142:                 }
143:
144:                 return super.isSlow(parent);
145:         }
146:
147:         @Override
148:         public void fillChildren(ECPContainer context, Object parent, InternalChildrenList childrenList) {
149:•                if (parent instanceof InternalProject) {
150:                         final InternalProject project = (InternalProject) parent;
151:                         final CDOProjectData projectData = getProjectData(project);
152:                         childrenList.addChildren(projectData.getRootResource().getContents());
153:•                } else if (parent instanceof InternalRepository) {
154:                         final InternalRepository repository = (InternalRepository) parent;
155:                         final CDOBranchWrapper wrapper = new CDOBranchWrapper(repository, CDOBranch.MAIN_BRANCH_NAME);
156:                         childrenList.addChild(wrapper);
157:•                } else if (parent instanceof CDOBranchWrapper) {
158:                         final CDOBranchWrapper parentWrapper = (CDOBranchWrapper) parent;
159:                         final InternalRepository repository = parentWrapper.getRepository();
160:                         final String branchPath = parentWrapper.getBranchPath();
161:
162:                         CDONet4jSession session = null;
163:
164:                         try {
165:                                 final CDORepositoryData repositoryData = getRepositoryData(repository);
166:                                 final CDONet4jSessionConfiguration sessionConfiguration = repositoryData.createSessionConfiguration();
167:                                 session = sessionConfiguration.openNet4jSession();
168:
169:                                 final CDOBranch branch = session.getBranchManager().getBranch(branchPath);
170:•                                for (final CDOBranch child : branch.getBranches()) {
171:                                         final CDOBranchWrapper wrapper = new CDOBranchWrapper(repository, child.getPathName());
172:                                         childrenList.addChild(wrapper);
173:                                 }
174:                         } finally {
175:                                 IOUtil.close(session);
176:                         }
177:                 } else {
178:                         super.fillChildren(context, parent, childrenList);
179:                 }
180:         }
181:
182:         /** {@inheritDoc} */
183:         @Override
184:         @SuppressWarnings("unchecked")
185:         public EList<Object> getElements(InternalProject project) {
186:                 final CDOProjectData data = (CDOProjectData) project.getProviderSpecificData();
187:                 return (EList<Object>) (EList<?>) data.getTransaction().getResourceSet().getResources();
188:                 // TODO: implement CDOProvider.addRootElement(project, rootElement)
189:                 // throw new UnsupportedOperationException();
190:                 // return new BasicEList<Object>();
191:         }
192:
193:         @Override
194:         public void handleLifecycle(ECPContainer context, LifecycleEvent event) {
195:                 super.handleLifecycle(context, event);
196:
197:•                if (context instanceof InternalProject) {
198:                         final InternalProject project = (InternalProject) context;
199:•                        switch (event) {
200:                         case CREATE:
201:                                 createProject(project);
202:                                 break;
203:
204:                         case INIT:
205:                                 break;
206:
207:                         case DISPOSE:
208:                                 disposeProject(project);
209:                                 break;
210:
211:                         case REMOVE:
212:                                 removeProject(project);
213:                                 break;
214:
215:                         default:
216:                                 break;
217:                         }
218:                 }
219:         }
220:
221:         /**
222:          * Creates a provider specific project for the given internal project.
223:          *
224:          * @param project the internal project
225:          */
226:         protected void createProject(InternalProject project) {
227:                 final String workspaceID = UUIDGenerator.DEFAULT.generate();
228:                 project.getProperties().addProperty(PROP_WORKSPACE_ID, workspaceID);
229:
230:                 final CDOProjectData projectData = getProjectData(project);
231:                 projectData.checkoutWorkspace();
232:                 final File folder = getProjectFolder(project);
233:                 PrintStream stream = null;
234:
235:                 try {
236:                         stream = new PrintStream(new File(folder, "ecp.properties")); //$NON-NLS-1$
237:                         stream.println("project.name = " + project.getName()); //$NON-NLS-1$
238:
239:                 } catch (final FileNotFoundException ex) {
240:                         Activator.log(ex);
241:                         throw new IllegalStateException("Retrieving project folder failed!", ex); //$NON-NLS-1$
242:                 } finally {
243:                         IOUtil.close(stream);
244:                 }
245:         }
246:
247:         /**
248:          * {@inheritDoc}
249:          *
250:          */
251:         @Override
252:         public ECPContainer getModelContext(Object element) {
253:•                if (element instanceof CDOResource) {
254:                         final Set<InternalProject> openProjects = getOpenProjects();
255:•                        for (final InternalProject project : openProjects) {
256:                                 final CDOProjectData projectData = (CDOProjectData) project.getProviderSpecificData();
257:•                                if (projectData.getRootResource().getContents().contains(element)) {
258:                                         return project;
259:                                 }
260:                         }
261:                 }
262:                 return super.getModelContext(element);
263:         }
264:
265:         /**
266:          * Create and store a {@link CDOWorkspaceConfiguration} for the given internal project.
267:          *
268:          * @param project the internal project.
269:          * @return the {@link CDOWorkspaceConfiguration}
270:          */
271:         protected CDOWorkspaceConfiguration createWorkspaceConfiguration(InternalProject project) {
272:                 final File folder = getProjectFolder(project);
273:                 folder.mkdirs();
274:
275:                 final IDBStore localStore = createLocalStore(project, folder);
276:                 final CDOWorkspaceBase base = createWorkspaceBase(project, folder);
277:                 final CDOSessionConfigurationFactory remote = getRepositoryData(project.getRepository());
278:
279:                 final ECPProperties properties = project.getProperties();
280:                 final String branchPath = properties.getValue(PROP_BRANCH_PATH);
281:                 final String timeStamp = properties.getValue(PROP_TIME_STAMP);
282:
283:                 final CDOWorkspaceConfiguration config = CDOWorkspaceUtil.createWorkspaceConfiguration();
284:                 config.setLocalRepositoryName(folder.getName()); // workspaceID
285:                 config.setStore(localStore);
286:                 config.setBase(base);
287:                 config.setRemote(remote);
288:•                config.setBranchPath(branchPath == null ? CDOBranch.MAIN_BRANCH_NAME : branchPath);
289:•                config.setTimeStamp(timeStamp == null ? CDOBranchPoint.UNSPECIFIED_DATE : Long.parseLong(timeStamp));
290:
291:                 return config;
292:         }
293:
294:         /**
295:          * Create and store a {@link CDOWorkspaceBase}.
296:          *
297:          * @param project the internal project.
298:          * @param folder the folder to put the {@link CDOWorkspaceBase} into
299:          * @return the {@link CDOWorkspaceBase}
300:          */
301:         protected CDOWorkspaceBase createWorkspaceBase(InternalProject project, File folder) {
302:                 final File base = new File(folder, "base"); //$NON-NLS-1$
303:                 base.mkdirs();
304:
305:                 return CDOWorkspaceUtil.createFolderWorkspaceBase(base);
306:         }
307:
308:         /**
309:          * Create a local {@link IDBStore}.
310:          *
311:          * @param project the internal project
312:          * @param folder the folder to store the data in
313:          * @return the {@link IDBStore}
314:          */
315:         protected IDBStore createLocalStore(InternalProject project, File folder) {
316:                 final File local = new File(folder, "local"); //$NON-NLS-1$
317:                 local.mkdirs();
318:
319:                 final JdbcDataSource dataSource = new JdbcDataSource();
320:                 dataSource.setURL("jdbc:h2:" + new File(local, "local").getAbsolutePath()); //$NON-NLS-1$ //$NON-NLS-2$
321:
322:                 final IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(false, false);
323:                 final IDBAdapter dbAdapter = new H2Adapter();
324:                 final IDBConnectionProvider dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
325:
326:                 return CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);
327:         }
328:
329:         /**
330:          * Dispose the given project and its data.
331:          *
332:          * @param project the internal project
333:          */
334:         protected void disposeProject(InternalProject project) {
335:                 final CDOProjectData data = (CDOProjectData) project.getProviderSpecificData();
336:                 data.dispose();
337:         }
338:
339:         /**
340:          * Remove the internal project and its configuration data.
341:          *
342:          * @param project the internal project
343:          */
344:         protected void removeProject(InternalProject project) {
345:                 final File folder = getProjectFolder(project);
346:•                if (folder.exists()) {
347:•                        if (!folder.delete()) {
348:                                 folder.deleteOnExit();
349:                         }
350:                 }
351:         }
352:
353:         /**
354:          * Retrieve {@link CDORepositoryData} for a given {@link InternalRepository}.
355:          *
356:          * @param repository the internal repositorz
357:          * @return the {@link CDORepositoryData}
358:          */
359:         public static CDORepositoryData getRepositoryData(InternalRepository repository) {
360:                 synchronized (repository) {
361:                         CDORepositoryData data = (CDORepositoryData) repository.getProviderSpecificData();
362:•                        if (data == null) {
363:                                 data = new CDORepositoryData(repository);
364:                                 repository.setProviderSpecificData(data);
365:                         }
366:
367:                         return data;
368:                 }
369:         }
370:
371:         /**
372:          * Get {@link CDOProjectData} for the given internal project.
373:          *
374:          * @param project the internal project
375:          * @return the {@link CDOProjectData}
376:          */
377:         public static CDOProjectData getProjectData(InternalProject project) {
378:                 synchronized (project) {
379:                         CDOProjectData data = (CDOProjectData) project.getProviderSpecificData();
380:•                        if (data == null) {
381:                                 data = new CDOProjectData(project);
382:                                 project.setProviderSpecificData(data);
383:                         }
384:
385:                         return data;
386:                 }
387:         }
388:
389:         /**
390:          * Get the folder for the configuration data of the internal project.
391:          *
392:          * @param project the internal project.
393:          * @return the {@link File}
394:          */
395:         public static File getProjectFolder(InternalProject project) {
396:                 final String workspaceID = project.getProperties().getValue(PROP_WORKSPACE_ID);
397:                 return new File(Activator.getInstance().getStateLocation().toFile(), workspaceID);
398:         }
399:
400:         /** {@inheritDoc} */
401:         @Override
402:         public void cloneProject(final InternalProject projectToClone, InternalProject targetProject) {
403:                 throw new UnsupportedOperationException();
404:         }
405:
406:         /** {@inheritDoc} */
407:         @Override
408:         public Notifier getRoot(InternalProject project) {
409:                 final CDOProjectData data = (CDOProjectData) project.getProviderSpecificData();
410:•                if (data != null) {
411:                         return data.getRootResource();
412:                 }
413:                 return null;
414:         }
415:
416:         /**
417:          * {@inheritDoc}
418:          *
419:          * @see org.eclipse.emf.ecp.spi.core.InternalProvider#isThreadSafe()
420:          */
421:         @Override
422:         public boolean isThreadSafe() {
423:                 return true;
424:         }
425:
426:         /**
427:          * {@inheritDoc}
428:          *
429:          * @see org.eclipse.emf.ecp.spi.core.DefaultProvider#doDelete(org.eclipse.emf.ecp.spi.core.InternalProject,
430:          * java.util.Collection)
431:          */
432:         @Override
433:         public void doDelete(InternalProject project, Collection<Object> objects) {
434:                 final CDOResource cdoResource = getProjectData(project).getRootResource();
435:                 cdoResource.getContents().removeAll(objects);
436:
437:         }
438: }