Java tutorial
/* * Copyright 2015 PRODYNA AG * * Licensed under the Eclipse Public License (EPL), Version 1.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the License at * * https://www.eclipse.org/legal/epl-v10.html * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ package org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino; import java.util.Date; import org.alfresco.util.PropertyCheck; import org.mozilla.javascript.Context; import org.mozilla.javascript.IdScriptableObject; import org.mozilla.javascript.ScriptRuntime; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; import org.nabucco.alfresco.enhScriptEnv.common.script.converter.ValueConverter; import org.nabucco.alfresco.enhScriptEnv.common.script.converter.ValueInstanceConverterRegistry; import org.nabucco.alfresco.enhScriptEnv.common.script.converter.ValueInstanceConverterRegistry.ValueInstanceConverter; import org.springframework.beans.factory.InitializingBean; /** * @author Axel Faust, <a href="http://www.prodyna.com">PRODYNA AG</a> */ public class DateConverter implements ValueInstanceConverter, InitializingBean { private static final String TYPE_DATE = "Date"; private static final Scriptable DUMMY_SCOPE; static { final Context cx = Context.enter(); try { DUMMY_SCOPE = cx.initStandardObjects(null, true); DUMMY_SCOPE.delete("Packages"); DUMMY_SCOPE.delete("getClass"); DUMMY_SCOPE.delete("java"); ((ScriptableObject) DUMMY_SCOPE).sealObject(); } finally { Context.exit(); } } protected ValueInstanceConverterRegistry registry; /** * @param registry * the registry to set */ public void setRegistry(final ValueInstanceConverterRegistry registry) { this.registry = registry; } /** * * {@inheritDoc} */ @Override public void afterPropertiesSet() { PropertyCheck.mandatory(this, "registry", this.registry); this.registry.registerValueInstanceConverter(Date.class, this); this.registry.registerValueInstanceConverter(IdScriptableObject.class, this); } /** * * {@inheritDoc} */ @Override public int getForJavaConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if (IdScriptableObject.class.isAssignableFrom(valueInstanceClass) && expectedClass.isAssignableFrom(Date.class)) { confidence = MEDIUM_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; } /** * {@inheritDoc} */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final boolean canConvert = value instanceof IdScriptableObject && TYPE_DATE.equals(((IdScriptableObject) value).getClassName()) && expectedClass.isAssignableFrom(Date.class); return canConvert; } /** * {@inheritDoc} */ @Override public Object convertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final Object result = Context.jsToJava(value, Date.class); return result; } /** * * {@inheritDoc} */ @Override public int getForScriptConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if (Date.class.isAssignableFrom(valueInstanceClass) && expectedClass.isAssignableFrom(IdScriptableObject.class)) { confidence = MEDIUM_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; } /** * {@inheritDoc} */ @Override public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final boolean canConvert = value instanceof Date && expectedClass.isAssignableFrom(IdScriptableObject.class); return canConvert; } /** * {@inheritDoc} */ @Override public Object convertValueForScript(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { if (!(value instanceof Date)) { throw new IllegalArgumentException("value must be a date"); } Context.enter(); try { // Note: Context.javaToJS actually does not handle Date in a way that would make them "native Dates" final Object result = ScriptRuntime.newObject(Context.getCurrentContext(), DUMMY_SCOPE, TYPE_DATE, new Object[] { Long.valueOf(((Date) value).getTime()) }); return result; } finally { Context.exit(); } } }