Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools')
-rw-r--r--bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/IMessageFactoryService.java25
-rw-r--r--bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Message.java14
-rw-r--r--bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Translation.java16
-rw-r--r--bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/MessageFactoryServiceImpl.java247
-rw-r--r--bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/TranslationObjectSupplier.java173
5 files changed, 0 insertions, 475 deletions
diff --git a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/IMessageFactoryService.java b/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/IMessageFactoryService.java
deleted file mode 100644
index 94080225..00000000
--- a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/IMessageFactoryService.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package org.eclipse.e4.tools.services;
-
-import java.util.Locale;
-import java.util.ResourceBundle;
-
-import org.eclipse.osgi.service.localization.BundleLocalization;
-
-/**
- * Service that is responsible for creating and managing message class instances.
- */
-public interface IMessageFactoryService {
-
- /**
- * Returns an instance of the of a given messages class for the given {@link Locale}.
- * If configured it caches the created instances and return the already created instances.
- * Otherwise a new instance will be created.
- *
- * @param locale The {@link Locale} for which the message class instance is requested.
- * @param messages The type of the message class whose instance is requested.
- * @param localization The service that is needed to retrieve {@link ResourceBundle} objects from a bundle
- * with a given locale.
- * @return An instance of the given messages class and {@link Locale}.
- */
- public <M> M getMessageInstance(final Locale locale, final Class<M> messages, BundleLocalization localization);
-}
diff --git a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Message.java b/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Message.java
deleted file mode 100644
index 05afcf7f..00000000
--- a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Message.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.eclipse.e4.tools.services;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-
-@Retention(RetentionPolicy.RUNTIME)
-public @interface Message {
- public enum ReferenceType {
- NONE, SOFT, WEAK
- }
-
- ReferenceType referenceType() default ReferenceType.SOFT;
- String contributorURI() default "";
-}
diff --git a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Translation.java b/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Translation.java
deleted file mode 100644
index ca92d445..00000000
--- a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/Translation.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package org.eclipse.e4.tools.services;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-import javax.inject.Qualifier;
-
-@Qualifier
-@Documented
-@Target({ElementType.FIELD, ElementType.PARAMETER})
-@Retention(RetentionPolicy.RUNTIME)
-public @interface Translation {
-
-}
diff --git a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/MessageFactoryServiceImpl.java b/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/MessageFactoryServiceImpl.java
deleted file mode 100644
index 4273532f..00000000
--- a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/MessageFactoryServiceImpl.java
+++ /dev/null
@@ -1,247 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2011 BestSolution.at and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
- * Dirk Fauth <dirk.fauth@gmail.com> - modifications to instance creation
- ******************************************************************************/
-package org.eclipse.e4.tools.services.impl;
-
-import java.lang.ref.Reference;
-import java.lang.ref.SoftReference;
-import java.lang.ref.WeakReference;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-
-import javax.annotation.PostConstruct;
-
-import org.eclipse.e4.tools.services.IMessageFactoryService;
-import org.eclipse.e4.tools.services.Message;
-import org.eclipse.e4.tools.services.Message.ReferenceType;
-import org.eclipse.e4.tools.services.ToolsServicesActivator;
-import org.eclipse.osgi.service.localization.BundleLocalization;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.service.log.LogService;
-
-public class MessageFactoryServiceImpl implements IMessageFactoryService {
-
- private static LogService logService = ToolsServicesActivator.getDefault().getLogService();
-
- // Cache so when multiple instance use the same message class
- private Map<Object, Reference<Object>> SOFT_CACHE = Collections
- .synchronizedMap(new HashMap<Object, Reference<Object>>());
-
- private Map<Object, Reference<Object>> WEAK_CACHE = Collections
- .synchronizedMap(new HashMap<Object, Reference<Object>>());
-
- private int CLEANUPCOUNT = 0;
-
- @Override
- public <M> M getMessageInstance(final Locale locale, final Class<M> messages, final BundleLocalization localization) {
- String key = messages.getName() + "_" + locale; //$NON-NLS-1$
-
- final Message annotation = messages.getAnnotation(Message.class);
- Map<Object, Reference<Object>> cache = null;
- ReferenceType type = ReferenceType.NONE;
-
- if (++CLEANUPCOUNT > 1000) {
- Iterator<Entry<Object, Reference<Object>>> it = WEAK_CACHE
- .entrySet().iterator();
- while (it.hasNext()) {
- if (it.next().getValue().get() == null) {
- it.remove();
- }
- }
-
- it = SOFT_CACHE.entrySet().iterator();
- while (it.hasNext()) {
- if (it.next().getValue().get() == null) {
- it.remove();
- }
- }
- CLEANUPCOUNT = 0;
- }
-
- if (annotation == null
- || annotation.referenceType() == ReferenceType.SOFT) {
- cache = SOFT_CACHE;
- type = ReferenceType.SOFT;
- } else if (annotation.referenceType() == ReferenceType.WEAK) {
- cache = WEAK_CACHE;
- type = ReferenceType.WEAK;
- }
-
- if (cache != null && cache.containsKey(key)) {
- @SuppressWarnings("unchecked")
- Reference<M> ref = (Reference<M>) cache.get(key);
- M o = ref.get();
- if (o != null) {
- return o;
- }
- cache.remove(key);
- }
-
- M instance;
-
- if (System.getSecurityManager() == null) {
- instance = createInstance(locale, messages, annotation, localization);
- } else {
- instance = AccessController.doPrivileged(new PrivilegedAction<M>() {
-
- public M run() {
- return createInstance(locale, messages, annotation, localization);
- }
-
- });
- }
-
- if (cache != null) {
- if (type == ReferenceType.SOFT) {
- cache.put(key, new SoftReference<Object>(instance));
- } else if (type == ReferenceType.WEAK) {
- cache.put(key, new WeakReference<Object>(instance));
- }
- }
-
- return instance;
- }
-
- /**
- * Creates and returns an instance of the of a given messages class for the given {@link Locale}.
- * The message class gets instantiated and the fields are initialized with values out of a {@link ResourceBundle}.
- * As there are several options to specify the location of the {@link ResourceBundle} to load, the
- * following search order is used:
- * <ol>
- * <li>URI location<br/>
- * If the message class is annotated with <code>@Message</code> and the <i>contributorURI</i>
- * attribute is set, the {@link ResourceBundle} is searched at the specified location</li>
- * <li>Relative location<br/>
- * If the message class is not annotated with <code>@Message</code> and a contributorURI
- * attribute value or there is no {@link ResourceBundle} found at the specified location, a
- * {@link ResourceBundle} with the same name in the same package as the message class is searched.</li>
- * <li>Bundle localization<br/>
- * If there is no {@link ResourceBundle} found by URI or relative location, the OSGi {@link ResourceBundle}
- * configured in the MANIFEST.MF is tried to load.</li>
- * </ol>
- * Note: Even if there is no {@link ResourceBundle} found in any of the mentioned locations, this method will
- * not break. In this case the fields of the message class will get initialized with values that look
- * like <code>!key!</code> to indicate that there is no translation value found for that key.
- *
- * @param locale The {@link Locale} for which the message class instance is requested.
- * @param messages The type of the message class whose instance is requested.
- * @param annotation The annotation that is used in the message class. If specified it is needed
- * to retrieve the URI of the location to search for the {@link ResourceBundle}.
- * @param localization The service that is needed to retrieve {@link ResourceBundle} objects from a bundle
- * with a given locale.
- *
- * @return The created instance of the given messages class and {@link Locale} or <code>null</code>
- * if an error occured on creating the instance.
- */
- private static <M> M createInstance(Locale locale, Class<M> messages,
- Message annotation, BundleLocalization localization) {
-
- ResourceBundle resourceBundle = null;
- if (annotation != null && annotation.contributorURI().length() > 0) {
- resourceBundle = ResourceBundleHelper.getResourceBundleForUri(annotation.contributorURI(), locale, localization);
- }
-
- if (resourceBundle == null) {
- //check for the resource bundle relative to the messages class
- String baseName = messages.getName().replace('.', '/');
-
- try {
- resourceBundle = ResourceBundleHelper.getEquinoxResourceBundle(baseName, locale, messages.getClassLoader());
- }
- catch (MissingResourceException e) {
- //do nothing as this just means there is no resource bundle named
- //like the messages class in the same package
- //therefore we will go on and search for the OSGi resource bundle
- }
- }
-
- if (resourceBundle == null) {
- //retrieve the OSGi resource bundle
- Bundle bundle = FrameworkUtil.getBundle(messages);
- resourceBundle = localization.getLocalization(bundle, locale.toString());
- }
-
- //always create a provider, if there is no resource bundle found, simply the modified keys will
- //be returned by this provider to show that there is something wrong on loading it
- ResourceBundleTranslationProvider provider = new ResourceBundleTranslationProvider(resourceBundle);
-
- M instance = null;
- try {
- instance = messages.newInstance();
- Field[] fields = messages.getDeclaredFields();
-
- for (int i = 0; i < fields.length; i++) {
- if (!fields[i].isAccessible()) {
- fields[i].setAccessible(true);
- }
-
- fields[i].set(instance,
- provider.translate(fields[i].getName()));
- }
- } catch (InstantiationException e) {
- if (logService != null)
- logService.log(LogService.LOG_ERROR,
- "Instantiation of messages class failed", e); //$NON-NLS-1$
- } catch (IllegalAccessException e) {
- if (logService != null)
- logService.log(LogService.LOG_ERROR,
- "Failed to access messages class", e); //$NON-NLS-1$
- }
-
- //invoke the method annotated with @PostConstruct
- processPostConstruct(instance, messages);
-
- return instance;
- }
-
- /**
- * Searches for the method annotated {@link PostConstruct} in the messages class.
- * If there is one found it will be executed.
- * <p>
- * Note: The method annotated with {@link PostConstruct} does not support method injection
- * because we are not using the injection mechanism to call.
- * @param messageObject The message instance of the given class where the method annotated with
- * {@link PostConstruct} should be called
- * @param messageClass The type of the message class whose instance is requested.
- */
- private static void processPostConstruct(Object messageObject, Class<?> messageClass) {
- if (messageObject != null) {
- Method[] methods = messageClass.getDeclaredMethods();
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- if (!method.isAnnotationPresent(PostConstruct.class)) {
- continue;
- }
- else {
- try {
- method.invoke(messageObject);
- } catch (Exception e) {
- if (logService != null)
- logService.log(LogService.LOG_ERROR,
- "Exception on trying to execute the @PostConstruct annotated method in " + messageClass, e); //$NON-NLS-1$
- }
- }
- }
- }
- }
-
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/TranslationObjectSupplier.java b/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/TranslationObjectSupplier.java
deleted file mode 100644
index c52c2afd..00000000
--- a/bundles/org.eclipse.e4.tools.services/src/org/eclipse/e4/tools/services/impl/TranslationObjectSupplier.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2011 BestSolution.at and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
- * Dirk Fauth <dirk.fauth@gmail.com> - modifications to support locale changes at runtime
- ******************************************************************************/
-package org.eclipse.e4.tools.services.impl;
-
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
-import java.util.ResourceBundle;
-import java.util.Set;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-
-import org.eclipse.e4.core.di.annotations.Optional;
-import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
-import org.eclipse.e4.core.di.suppliers.IObjectDescriptor;
-import org.eclipse.e4.core.di.suppliers.IRequestor;
-import org.eclipse.e4.core.services.translation.TranslationService;
-import org.eclipse.e4.tools.services.IMessageFactoryService;
-import org.eclipse.e4.tools.services.Message;
-import org.eclipse.e4.tools.services.ToolsServicesActivator;
-import org.eclipse.osgi.service.localization.BundleLocalization;
-import org.osgi.service.log.LogService;
-
-@SuppressWarnings("rawtypes")
-public class TranslationObjectSupplier extends ExtendedObjectSupplier {
-
- private static LogService logService = ToolsServicesActivator.getDefault().getLogService();
-
- /**
- * The current active locale that gets injected for updating the message instances.
- */
- private Locale locale;
-
- /**
- * The service that gets {@link ResourceBundle} objects from a bundle with a given locale.
- */
- @Inject
- private BundleLocalization localization;
-
- /**
- * The service that creates instances of message classes based on the current active locale,
- * the {@link BundleLocalization} and the configuration used in the {@link Message} annotation.
- */
- @Inject
- private IMessageFactoryService factoryService;
-
- /**
- * Map that contains all {@link IRequestor} that requested an instance of a messages class.
- * Used to inform all requestor if the instances have changed due to a locale change.
- */
- private Map<Class, Set<IRequestor>> listeners = new HashMap<Class, Set<IRequestor>>();
-
- @Override
- public Object get(IObjectDescriptor descriptor, IRequestor requestor,
- boolean track, boolean group) {
-
- Class<?> descriptorsClass = getDesiredClass(descriptor.getDesiredType());
-
- if (track)
- addListener(descriptorsClass, requestor);
-
- return getMessageInstance(descriptorsClass);
- }
-
- /**
- * Setting the {@link Locale} by using this method will cause to create new instances for all
- * message classes that were requested before. It also notifys all {@link IRequestor} that requested
- * those messages instance which causes dynamic reinjection.
- * @param locale The {@link Locale} to use for creating the message instances.
- */
- @Inject
- public void setLocale(@Optional @Named(TranslationService.LOCALE) String locale) {
- try {
- this.locale = locale == null ? Locale.getDefault() : ResourceBundleHelper.toLocale(locale);
- }
- catch (IllegalArgumentException e) {
- //parsing the locale String to a Locale failed because of invalid String, use the default locale
- if (logService != null)
- logService.log(LogService.LOG_ERROR, e.getMessage() + " - Default Locale will be used instead."); //$NON-NLS-1$
- this.locale = Locale.getDefault();
- }
- catch (Exception e) {
- //parsing the locale String to a Locale failed, so we use the default Locale
- if (logService != null)
- logService.log(LogService.LOG_ERROR, "Invalid locale", e); //$NON-NLS-1$
- this.locale = Locale.getDefault();
- }
-
- //update listener
- updateMessages();
- }
-
- /**
- * Notify the {@link IRequestor}s of those instances that they need to update their message class instances.
- */
- private void updateMessages() {
- for (Map.Entry<Class, Set<IRequestor>> entry : this.listeners.entrySet()) {
- notifyRequestor(entry.getValue());
- }
- }
-
- /**
- * Checks if for the specified descriptor class there is already an instance in the local
- * cache. If not a new instance is created using the local configuration on {@link Locale},
- * {@link BundleLocalization} and given descriptor class.
- * @param descriptorsClass The class for which an instance is requested.
- * @return The instance of the requested message class
- */
- private Object getMessageInstance(Class<?> descriptorsClass) {
- return this.factoryService.getMessageInstance(this.locale, descriptorsClass, this.localization);
- }
-
- /**
- * Remember the {@link IRequestor} that requested an instance of the given descriptor class.
- * This is needed to be able to inform all {@link IRequestor} if the {@link Locale} changes
- * at runtime.
- * @param descriptorsClass The class for which an instance was requested.
- * @param requestor The {@link IRequestor} that requested the instance.
- */
- private void addListener(Class<?> descriptorsClass, IRequestor requestor) {
- Set<IRequestor> registered = this.listeners.get(descriptorsClass);
- if (registered == null) {
- registered = new HashSet<IRequestor>();
- this.listeners.put(descriptorsClass, registered);
- }
- registered.add(requestor);
- }
-
- /**
- * Notify all given {@link IRequestor} about changes for their injected values.
- * This way the dynamic injection is performed.
- * @param requestors The {@link IRequestor} to inform about the instance changes.
- */
- private void notifyRequestor(Collection<IRequestor> requestors) {
- if (requestors != null) {
- for (Iterator<IRequestor> it = requestors.iterator(); it.hasNext();) {
- IRequestor requestor = it.next();
- if (!requestor.isValid()) {
- it.remove();
- continue;
- }
- requestor.resolveArguments(false);
- requestor.execute();
- }
- }
- }
-
- private Class<?> getDesiredClass(Type desiredType) {
- if (desiredType instanceof Class<?>)
- return (Class<?>) desiredType;
- if (desiredType instanceof ParameterizedType) {
- Type rawType = ((ParameterizedType) desiredType).getRawType();
- if (rawType instanceof Class<?>)
- return (Class<?>) rawType;
- }
- return null;
- }
-} \ No newline at end of file

Back to the top