Skip to content

Package: ViewModelMigratorUtil

ViewModelMigratorUtil

nameinstructionbranchcomplexitylinemethod
getStringViewModelMigrator()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getViewModelMigrator()
M: 0 C: 5
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getViewModelWorkspaceMigrator()
M: 0 C: 18
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
loadClass(String, String)
M: 12 C: 9
43%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
log(Throwable)
M: 33 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
readExtensionPoint()
M: 14 C: 71
84%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 6 C: 20
77%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.migrator;
15:
16: import org.eclipse.core.runtime.IConfigurationElement;
17: import org.eclipse.core.runtime.IExtension;
18: import org.eclipse.core.runtime.IExtensionPoint;
19: import org.eclipse.core.runtime.InvalidRegistryObjectException;
20: import org.eclipse.core.runtime.Platform;
21: import org.eclipse.emf.ecp.spi.view.migrator.string.StringViewModelMigrator;
22: import org.eclipse.emfforms.common.ServiceObjectTracker;
23: import org.eclipse.emfforms.spi.common.report.AbstractReport;
24: import org.eclipse.emfforms.spi.common.report.ReportService;
25: import org.osgi.framework.Bundle;
26: import org.osgi.framework.BundleContext;
27: import org.osgi.framework.FrameworkUtil;
28: import org.osgi.framework.ServiceReference;
29:
30: /**
31: * @author Lucas Koehler
32: *
33: */
34: public final class ViewModelMigratorUtil {
35:
36:         private static final String MIGRATOR_EXTENSION = "org.eclipse.emf.ecp.view.migrator.migrators"; //$NON-NLS-1$
37:         private static final String MIGRATOR_CLASS = "class"; //$NON-NLS-1$
38:         private static final String MIGRATOR_PRIORITY = "priority"; //$NON-NLS-1$
39:
40:         private static ViewModelMigrator migrator;
41:
42:         private ViewModelMigratorUtil() {
43:
44:         }
45:
46:         private static void readExtensionPoint() {
47:                 final IExtensionPoint extensionPoint = Platform.getExtensionRegistry()
48:                         .getExtensionPoint(MIGRATOR_EXTENSION);
49:                 int topPriority = 0;
50:                 Class<ViewModelMigrator> topClass = null;
51:•                for (final IExtension extension : extensionPoint.getExtensions()) {
52:•                        for (final IConfigurationElement configurationElement : extension
53:                                 .getConfigurationElements()) {
54:                                 try {
55:
56:                                         final int priority = Integer.parseInt(configurationElement.getAttribute(MIGRATOR_PRIORITY));
57:•                                        if (priority > topPriority) {
58:                                                 final Class<ViewModelMigrator> migratorClass = loadClass(configurationElement
59:                                                         .getContributor().getName(),
60:                                                         configurationElement
61:                                                                 .getAttribute(MIGRATOR_CLASS));
62:                                                 topClass = migratorClass;
63:                                                 topPriority = priority;
64:                                         }
65:                                 } catch (final ClassNotFoundException ex) {
66:                                         log(ex);
67:                                 } catch (final InvalidRegistryObjectException ex) {
68:                                         log(ex);
69:                                 }
70:                         }
71:                 }
72:•                if (topClass != null) {
73:                         try {
74:                                 migrator = topClass.newInstance();
75:                         } catch (final InstantiationException ex) {
76:                                 log(ex);
77:                         } catch (final IllegalAccessException ex) {
78:                                 log(ex);
79:                         }
80:                 }
81:         }
82:
83:         /**
84:          * @return the view model migrator with the highest priority, <code>null</code> if no migrator was registered to the
85:          * extension point.
86:          * @since 1.8.0
87:          */
88:         public static ViewModelMigrator getViewModelMigrator() {
89:•                if (migrator == null) {
90:                         readExtensionPoint();
91:                 }
92:                 return migrator;
93:         }
94:
95:         /**
96:          *
97:          * @return the view model migrator with the highest priority if it also supports migrating string based view models.
98:          * @since 1.8
99:          */
100:         public static StringViewModelMigrator getStringViewModelMigrator() {
101:•                if (!StringViewModelMigrator.class.isInstance(getViewModelMigrator())) {
102:                         return null;
103:                 }
104:                 return StringViewModelMigrator.class.cast(getViewModelMigrator());
105:         }
106:
107:         /**
108:          * @return the view model workspace migrator
109:          * @since 1.8
110:          */
111:         public static ViewModelWorkspaceMigrator getViewModelWorkspaceMigrator() {
112:•                if (viewModelWorkspaceMigratorObjectTracker == null) {
113:                         final Bundle bundle = FrameworkUtil.getBundle(ViewModelMigratorUtil.class);
114:                         final BundleContext bundleContext = bundle.getBundleContext();
115:                         viewModelWorkspaceMigratorObjectTracker = new ServiceObjectTracker<ViewModelWorkspaceMigrator>(
116:                                 bundleContext, ViewModelWorkspaceMigrator.class);
117:                 }
118:                 return viewModelWorkspaceMigratorObjectTracker.getService();
119:         }
120:
121:         @SuppressWarnings("unchecked")
122:         private static <T> Class<T> loadClass(String bundleName, String clazz)
123:                 throws ClassNotFoundException {
124:                 final Bundle bundle = Platform.getBundle(bundleName);
125:•                if (bundle == null) {
126:                         throw new ClassNotFoundException(clazz + bundleName);
127:                 }
128:                 return (Class<T>) bundle.loadClass(clazz);
129:         }
130:
131:         private static void log(Throwable throwable) {
132:                 final Bundle bundle = FrameworkUtil.getBundle(ViewModelMigratorUtil.class);
133:                 final BundleContext bundleContext = bundle.getBundleContext();
134:                 final ServiceReference<ReportService> serviceReference = bundleContext.getServiceReference(ReportService.class);
135:•                if (serviceReference == null) {
136:                         return;
137:                 }
138:                 final ReportService reportService = bundleContext.getService(serviceReference);
139:                 try {
140:•                        if (reportService == null) {
141:                                 return;
142:                         }
143:                         reportService.report(new AbstractReport(throwable));
144:                 } finally {
145:                         bundleContext.ungetService(serviceReference);
146:                 }
147:         }
148:
149:         private static ServiceObjectTracker<ViewModelWorkspaceMigrator> viewModelWorkspaceMigratorObjectTracker;
150: }