001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.platform.el; 020 021import org.nuxeo.ecm.core.api.NuxeoException; 022import org.nuxeo.runtime.model.ComponentContext; 023import org.nuxeo.runtime.model.ComponentInstance; 024import org.nuxeo.runtime.model.DefaultComponent; 025 026import java.util.ArrayList; 027import java.util.List; 028 029import javax.el.ELContext; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033 034/** 035 * Implementation for the service providing access to EL-related functions. 036 * 037 * @since 8.3 038 */ 039public class ELServiceComponent extends DefaultComponent implements ELService { 040 041 private static final Log log = LogFactory.getLog(ELServiceComponent.class); 042 043 private static final String XP_EL_CONTEXT_FACTORY = "elContextFactory"; 044 045 protected static final ELContextFactory DEFAULT_EL_CONTEXT_FACTORY = new DefaultELContextFactory(); 046 047 protected List<ELContextFactoryDescriptor> elContextFactoryDescriptors; 048 049 protected ELContextFactory elContextFactory = DEFAULT_EL_CONTEXT_FACTORY; 050 051 @Override 052 public void activate(ComponentContext context) { 053 elContextFactoryDescriptors = new ArrayList<>(1); 054 } 055 056 @Override 057 public void deactivate(ComponentContext context) { 058 elContextFactoryDescriptors.clear(); 059 } 060 061 @Override 062 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 063 if (XP_EL_CONTEXT_FACTORY.equals(extensionPoint)) { 064 ELContextFactoryDescriptor desc = (ELContextFactoryDescriptor) contribution; 065 log.info("Registered ELContextFactory: " + desc.klass.getName()); 066 registerELContextFactoryDescriptor(desc); 067 } else { 068 throw new NuxeoException("Unknown extension point: " + extensionPoint); 069 } 070 } 071 072 @Override 073 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 074 if (XP_EL_CONTEXT_FACTORY.equals(extensionPoint)) { 075 ELContextFactoryDescriptor desc = (ELContextFactoryDescriptor) contribution; 076 log.info("Unregistered ELContextFactory: " + desc.klass.getName()); 077 unregisterELContextFactoryDescriptor(desc); 078 } else { 079 throw new NuxeoException("Unknown extension point: " + extensionPoint); 080 } 081 } 082 083 public void registerELContextFactoryDescriptor(ELContextFactoryDescriptor desc) { 084 elContextFactoryDescriptors.add(desc); 085 elContextFactory = desc.newInstance(); 086 } 087 088 public void unregisterELContextFactoryDescriptor(ELContextFactoryDescriptor desc) { 089 elContextFactoryDescriptors.remove(desc); 090 if (elContextFactoryDescriptors.isEmpty()) { 091 elContextFactory = DEFAULT_EL_CONTEXT_FACTORY; 092 } else { 093 desc = elContextFactoryDescriptors.get(elContextFactoryDescriptors.size() - 1); 094 elContextFactory = desc.newInstance(); 095 } 096 } 097 098 @Override 099 public ELContext createELContext() { 100 return elContextFactory.get(); 101 } 102 103}