4 Replies Latest reply on Jun 1, 2011 5:38 AM by ffang

    FUSE with Apache Velocity project

    carlos.baez

      Hi all,

       

      I am working in project where we are using FUSE and we are using apache velocity to load some necessary templates. Our project with includes all the methods and classes to work with velocity, also it has a velocity.properties from which we load our classloader. The file is this:

       

      runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem

      runtime.log.logsystem.log4j.category=org.apache.velocity.runtime.log.SimpleLog4JLogSystem

       

      resource.loader= class

       

      class.resource.loader.description=Personalize Resource Loader

      class.resource.loader.class=net.i2cat.mantychore.commandsets.junos.velocity.PersonalizeClassLoader

       

      My problem is that when we are loading the properties (the velocity.properties was read fine),  the PersonalizeClassLoader couldnt be loaded

       

      My method to initialize velocity:

       

              InputStream velocityPropFile = getClass().getResourceAsStream(VELOCITY_PROPS);

              if (velocityPropFile == null)

                  throw new ResourceNotFoundException("Cannot load: " + VELOCITY_PROPS);

       

              Properties prop = new Properties();

              prop.load(velocityPropFile);

       

              Velocity.init(prop);

       

      Error:

       

      Problem instantiating the template loader: net.i2cat.mantychore.commandsets.junos.velocity.PersonalizeClassLoader.

      Look at your properties file and make sure the

      name of the template loader is correct.

      net.i2cat.mantychore.commandsets.junos.velocity.PersonalizeClassLoader

      java.lang.ClassNotFoundException: net.i2cat.mantychore.commandsets.junos.velocity.PersonalizeClassLoader

          at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:506)

          at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)

          at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)

          at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)

          at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

          at java.lang.Class.forName0(Native Method)

          at java.lang.Class.forName(Class.java:169)

          at org.apache.velocity.util.ClassUtils.getClass(ClassUtils.java:77)

          at org.apache.velocity.util.ClassUtils.getNewInstance(ClassUtils.java:96)

          at org.apache.velocity.runtime.resource.loader.ResourceLoaderFactory.getLoader(ResourceLoaderFactory.java:49)

          at org.apache.velocity.runtime.resource.ResourceManagerImpl.initialize(ResourceManagerImpl.java:134)

          at org.apache.velocity.runtime.RuntimeInstance.initializeResourceManager(RuntimeInstance.java:661)

          at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:251)

          at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:589)

          at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:229)

          at org.apache.velocity.app.Velocity.init(Velocity.java:107)

          at net.i2cat.mantychore.commandsets.junos.velocity.VelocityEngine.init(VelocityEngine.java:48)

          at net.i2cat.mantychore.commandsets.junos.velocity.VelocityEngine.mergeTemplate(VelocityEngine.java:53)

          at net.i2cat.mantychore.actionsets.junos.actions.JunosAction.prepareVelocityCommand(JunosAction.java:31)

          at net.i2cat.mantychore.actionsets.junos.actions.SetInterfaceAction.prepareMessage(SetInterfaceAction.java:46)

          at net.i2cat.mantychore.actionsets.junos.actions.JunosAction.execute(JunosAction.java:53)

          at net.i2cat.mantychore.queuemanager.QueueManager.execute(QueueManager.java:68)

          at net.i2cat.mantychore.capability.chassis.shell.SetInterfaceCommand.doExecute(SetInterfaceCommand.java:68)

          at org.apache.karaf.shell.console.OsgiCommandSupport.execute(OsgiCommandSupport.java:41)

          at org.apache.felix.gogo.commands.basic.AbstractCommand.execute(AbstractCommand.java:35)

          at org.apache.felix.gogo.runtime.shell.CommandProxy.execute(CommandProxy.java:50)

          at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:229)

          at org.apache.felix.gogo.runtime.shell.Closure.executeStatement(Closure.java:162)

          at org.apache.felix.gogo.runtime.shell.Pipe.run(Pipe.java:101)

          at org.apache.felix.gogo.runtime.shell.Closure.execute(Closure.java:79)

          at org.apache.felix.gogo.runtime.shell.CommandSessionImpl.execute(CommandSessionImpl.java:71)

          at org.apache.karaf.shell.console.jline.Console.run(Console.java:180)

          at java.lang.Thread.run(Thread.java:662)

       

       

       

      It is very curious because we had been testing this project from pax-exam tool which emulate a equinox container and it worked fine...

       

      Link to our velocity apache - http://velocity.apache.org/

      Link to pax-exam tool - http://wiki.ops4j.org/display/paxexam/Pax+Exam

       

      Has some person an idea?

       

      While, i will read osgi documentation to find the possible problem....

       

      Regads,

      Carlos

        • 1. Re: FUSE with Apache Velocity project
          ffang

          Hi,

           

          I believe you hit an usual problem when use Class.forName in OSGi container, that said, the bundle having code like Class.forName(velocity bundle in this case) doesn't import the class package that it try to initialize(and velocity bundle is impossible to import all potential package beforehand). This is also a usual case when try to load jdbc driver with Class.forname.

           

          The solution could be

          1. if you can change velocity bundle, add Dynamic-Import OSGi metadate header.

           

          OR

           

          2. create a fragment bundle which contain the net.i2cat.mantychore.commandsets.junos.velocity.PersonalizeClassLoader and attach it to velocity bundle, so that any resource in fragment bundle is available for the host bundle.

           

          Freeman

          • 2. Re: FUSE with Apache Velocity project
            carlos.baez

            Method from velocity to read loaders (in this case, our personalizeclassloader) .... I am trying to fix the problem

             

                public static Class getClass(String clazz) throws ClassNotFoundException

                {

                    /**

                      

            • Use the Thread context classloader if possible

                     */

                    ClassLoader loader = Thread.currentThread().getContextClassLoader();

                    if (loader != null)

                    {

                        try

                        {

                            return Class.forName(clazz, true, loader);

                        }

                        catch (ClassNotFoundException E)

                        {

                            /**

                              

            • If not found with ThreadContext loader, fall thru to

                              

            • try System classloader below (works around bug in ant).

                             */

                        }

                    }

                    /**

                      

            • Thread context classloader isn't working out, so use system loader.

                     */

                    return Class.forName(clazz);

                }

            • 3. Re: FUSE with Apache Velocity project
              carlos.baez

              Fixed, the problem was there. I created a fragment project and it works fine. I mark as answered

               

              Thanks!!

              Carlos

              • 4. Re: FUSE with Apache Velocity project
                ffang

                Hi,

                 

                Great to know you're using fragment bundle to make it work.

                 

                Btw, we add DynamicImport for velocity bundles hosted in servicemix[1], so in the future you needn't use fragment bundle yourself, hopefully this can make things more easy.

                 

                https://issues.apache.org/jira/browse/SMX4-851

                 

                Freeman