>
> hi!

i have written a class thta does all the dirty work but where should a put 
the call to execute it?

in org.h2.engine.Database.open() ?
----
        getLobStorage().init();
        systemSession.commit(true);
        
        FnRegUtil.registerFromClasspath(trace, 
this.getClass().getClassLoader(), systemSession, mainSchema, null);
        
        trace.info("opened {0}", databaseName);
        if (checkpointAllowed > 0) {
            afterWriting();
        }
    }

----
like this ? 


c

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/h2-database/-/17E6i7yUAnMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

/*
 * Copyright 2012 H2 Group. Multiple-Licensed under the H2 License,
 * Version 1.0, and under the Eclipse Public License, Version 1.0
 * (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
package org.h2.ext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Collections;

import org.h2.command.ddl.CreateAggregate;
import org.h2.command.ddl.CreateFunctionAlias;
import org.h2.engine.Session;
import org.h2.message.Trace;
import org.h2.schema.Schema;

public abstract class FnRegUtil 
{
	public static final String META_INF_NAME = "/META-INF/org.h2.ext.FunctionRegistry";
	public static final String SYS_PROP_AUTOREG = "org.h2.ext.fnAutoreg";
	public static final String JDBC_PROP_AUTOREG = "extFnAutoreg";
	
	public static final boolean registerFunction(Trace trace, ClassLoader cl, Session session, Schema schema, String clazz, String method, String alias) throws ClassNotFoundException
	{
		CreateFunctionAlias cfn = new CreateFunctionAlias(session, schema);
		cfn.setIfNotExists(true);
		cfn.setJavaClassMethod(clazz+"."+method);
		cfn.setAliasName(alias.toUpperCase());
		cfn.setDeterministic(true);
		try { 
			cfn.update();
			trace.info("registered function {0} of {1}/{2}", alias.toUpperCase(), clazz, method);
		} catch(Exception xe) { 
			trace.info("registration error: function {0} of {1}/{2} -- {3}", alias.toUpperCase(), clazz, method, xe.toString());
			return false;
		}
		return true;
	}

	public static final boolean registerAggregate(Trace trace, ClassLoader cl, Session session, Schema schema, String clazz, String alias) throws ClassNotFoundException
	{
		CreateAggregate ca = new CreateAggregate(session);
		ca.setIfNotExists(true);
		ca.setJavaClassMethod(clazz);
		ca.setName(alias.toUpperCase());
		ca.setSchema(schema);
		try { 
			ca.update(); 
			trace.info("registered aggregate {0} of {1}", alias.toUpperCase(), clazz);
		} 
		catch(Exception xe) 
		{ 
			trace.info("registration error: aggregate {0} of {1} -- {2}", alias.toUpperCase(), clazz, xe.toString());
			return false;
		}
		return true;
	}
	
	public static final boolean register(Trace trace, ClassLoader cl, Session session, Schema schema, String clazz) throws ClassNotFoundException
	{
		String alias = null;
		clazz = clazz.trim();
		
		if(clazz.lastIndexOf(' ')>0)
		{
			alias = clazz.substring(clazz.lastIndexOf(' ')+1).toUpperCase();
			clazz = clazz.substring(0, clazz.lastIndexOf(' ')).trim();
		}
		else
		{
			alias = clazz.substring(clazz.lastIndexOf('.')+1).toUpperCase();
		}
		
		Class cla = cl.loadClass(clazz);
		
		for(Class x : cla.getInterfaces())
		{
			if("org.h2.api.AggregateFunction".equalsIgnoreCase(x.getCanonicalName()))
			{
				return registerAggregate(trace, cl, session, schema, clazz, alias);
			}
		}
		
		// not an aggreagate impl
		for(Method m : cla.getDeclaredMethods())
		{
			if(m.getName().toUpperCase().startsWith("FN_") && m.isAccessible())
			{
				registerFunction(trace, cl, session, schema, clazz, m.getName(), m.getName().substring(3));
			}
		}
				
		return true;
	}

	public static final boolean registerFromClasspath(Trace trace, ClassLoader cl, Session session, Schema schema, String tag) throws ClassNotFoundException, IOException
	{
		boolean procEnabled = Boolean.parseBoolean(System.getProperty(SYS_PROP_AUTOREG, "false").trim());

		if(procEnabled || tag!=null)
		{
			String metaName = META_INF_NAME;
	
			if(tag!=null && !"".equals(tag))
			{
				metaName += "."+(tag.toUpperCase());
			}
			
			for(URL url : Collections.list(cl.getResources(metaName)))
			{
				trace.info("found function registration at {0}", url.toString());
				BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
				try
				{
					String line = null;
					while((line = in.readLine()) != null)
					{
						line=line.trim();
						if(!"".equals(line))
						{
							trace.info("registration of {0}", line);
							register(trace, cl, session, schema, line);
						}
					}
				}
				catch(Exception xe)
				{
					
				}
				finally
				{
					in.close();
				}
			}
		}
		
		return procEnabled;
	}
}

Reply via email to