Package org.exoplatform.webui.config

Source Code of org.exoplatform.webui.config.Param

/**
* Copyright (C) 2009 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.exoplatform.webui.config;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

import org.exoplatform.resolver.ResourceResolver;
import org.exoplatform.webui.application.WebuiRequestContext;
import org.exoplatform.xml.object.XMLObject;

import java.io.InputStream;

public class Param
{

   private String name;

   private String value;

   private transient Object object;

   public String getName()
   {
      return name;
   }

   public void setName(String name)
   {
      this.name = name;
   }

   public String getValue()
   {
      return value;
   }

   public void setValue(String value)
   {
      this.value = value;
   }

   public Object getMapXMLObject(WebuiRequestContext context) throws Exception
   {
      if (object != null)
         return object;
      ResourceResolver resolver = context.getResourceResolver(value);
      InputStream is = resolver.getInputStream(value);
      object = XMLObject.getObject(is);
      is.close();
      return object;
   }

   @SuppressWarnings("unchecked")
   public <T> T getMapGroovyObject(WebuiRequestContext context) throws Exception
   {
      try
      {
         if (object != null)
            return (T)object;
         ResourceResolver resolver = context.getResourceResolver(value);
         InputStream is = resolver.getInputStream(value);
         //TODO if is == null throw an exception saying the it's impossible to find the file
         Binding binding = new Binding();
         GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader(), binding);
         object = shell.evaluate(is);
         is.close();
         return (T)object;
      }
      catch (Exception ex)
      {
         System.out.println("A  problem in the groovy script : " + value);
         ex.printStackTrace();
         throw ex;
      }
   }

   public <T> T getFreshObject(WebuiRequestContext context) throws Exception
   {
      try
      {
         ResourceResolver resolver = context.getResourceResolver(value);
         InputStream is = resolver.getInputStream(value);
         Binding binding = new Binding();
         GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader(), binding);
         object = shell.evaluate(is);
         is.close();
         return (T)object;
      }
      catch (Exception ex)
      {
         System.out.println("A  problem in the groovy script : " + value);
         ex.printStackTrace();
         throw ex;
      }
   }
}
TOP

Related Classes of org.exoplatform.webui.config.Param

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.