| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.runtime.jsr303.validation; |
| |
| import java.util.Locale; |
| import java.util.ResourceBundle; |
| |
| import org.apache.bval.jsr303.DefaultMessageInterpolator; |
| import org.eclipse.osbp.runtime.common.i18n.II18nService; |
| import org.eclipse.osbp.runtime.jsr303.validation.common.ValidationUtil; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.FrameworkUtil; |
| import org.osgi.framework.ServiceReference; |
| import org.osgi.util.tracker.ServiceTracker; |
| import org.osgi.util.tracker.ServiceTrackerCustomizer; |
| |
| public class OSGiMessageInterpolator extends DefaultMessageInterpolator |
| implements ServiceTrackerCustomizer<II18nService, II18nService> { |
| |
| private Locale locale; |
| private ServiceTracker<II18nService, II18nService> serviceTracker; |
| private BundleContext context; |
| private II18nService i18nService; |
| |
| public OSGiMessageInterpolator() { |
| super(); |
| |
| setupI18nService(); |
| } |
| |
| public OSGiMessageInterpolator(ResourceBundle resourceBundle) { |
| super(resourceBundle); |
| |
| setupI18nService(); |
| } |
| |
| @Override |
| public String interpolate(String message, Context context, Locale locale) { |
| |
| String result = ""; |
| if (i18nService != null) { |
| result = i18nService.getValue( |
| ValidationUtil.extractMessage(message), locale); |
| if (result != null && !result.trim().equals("")) { |
| result = ValidationUtil |
| .replaceMessageParameter(result, context); |
| return result; |
| } |
| } |
| |
| return super.interpolate(message, context, locale); |
| } |
| |
| @Override |
| public String interpolate(String message, Context context) { |
| return super.interpolate(message, context); |
| } |
| |
| @Override |
| public void setLocale(Locale locale) { |
| super.setLocale(locale); |
| |
| this.locale = locale; |
| } |
| |
| private void setupI18nService() { |
| context = FrameworkUtil.getBundle(getClass()).getBundleContext(); |
| serviceTracker = new ServiceTracker<II18nService, II18nService>( |
| context, II18nService.class, this); |
| serviceTracker.open(); |
| } |
| |
| @Override |
| public II18nService addingService(ServiceReference<II18nService> reference) { |
| if (i18nService == null) { |
| i18nService = context.getService(reference); |
| } |
| return null; |
| } |
| |
| @Override |
| public void modifiedService(ServiceReference<II18nService> reference, |
| II18nService service) { |
| |
| } |
| |
| @Override |
| public void removedService(ServiceReference<II18nService> reference, |
| II18nService service) { |
| if (this.i18nService == service) { |
| this.i18nService = null; |
| } |
| } |
| |
| } |