在插件里应用hsqldb和hibernate

最近计划用插件化的hibernate和嵌入式数据库来完成一些功能,目前使用的数据库是HsqlDB和Derby。

hibernate独立一个插件出来,mapping信息在本插件内维护,数据库的cfg信息则由数据库插件维护。

要解决的事情很多,比如hibernate的在插件里的寻址问题和数据库的配置问题。

 

刚刚开始这些工作,写一些心得体会。

先说HsqlDB在插件里的配置方法:

需要一个hsqldb包,我使用的版本是hsqldb-1.8.0.1.jar,把该jar包放置在插件的lib文件夹下,然后在Manifest.MF文件->Runtime->ClassPath处导入,之后Exported Packages处暴露出org.hsqldb(这是为了能让引用本插件的插件能够找到数据库驱动)

 

我们可以看看hsqldb的说明文档,可以得知org.hsqldb.Server是数据库的启动位置,它包含一个main方法,如下:

View Code
    /**
     * Creates and starts a new Server.  <p>
     *
     * Allows starting a Server via the command line interface. <p>
     *
     * @param args the command line arguments for the Server instance
     */
    public static void main(String[] args) {

        String propsPath = FileUtil.canonicalOrAbsolutePath("server");
        HsqlProperties fileProps =
            ServerConfiguration.getPropertiesFromFile(propsPath);
        HsqlProperties props = fileProps == null ? new HsqlProperties()
                                                 : fileProps;
        HsqlProperties stringProps = HsqlProperties.argArrayToProps(args,
            ServerConstants.SC_KEY_PREFIX);

        if (stringProps != null) {
            if (stringProps.getErrorKeys().length != 0) {
                printHelp("server.help");

                return;
            }

            props.addProperties(stringProps);
        }

        ServerConfiguration.translateDefaultDatabaseProperty(props);

        // Standard behaviour when started from the command line
        // is to halt the VM when the server shuts down.  This may, of
        // course, be overridden by whatever, if any, security policy
        // is in place.
        ServerConfiguration.translateDefaultNoSystemExitProperty(props);

        // finished setting up properties;
        Server server = new Server();

        server.setProperties(props);

        // now messages go to the channel specified in properties
        server.print("Startup sequence initiated from main() method");

        if (fileProps != null) {
            server.print("Loaded properties from [" + propsPath
                         + ".properties]");
        } else {
            server.print("Could not load properties from file");
            server.print("Using cli/default properties only");
        }

        server.start();
    }

我们可以为它提供各种选项,比如-port -database.? -dbname.?等

我们可以新建一个服务类,提供出start和stop方法,在start方法中调用Server.main(XX)即可。

这样就配置好了hsqlDB

 

再来是hibernate配置,相对要复杂一点。

需要的包可以参考上一篇文章,把这些jar包放入lib文件夹内,导入它们。

提供一个继承自org.hibernate.cfg.Configuration的MyConfiguration

重写以下方法(测试用代码,仅供参考):

    protected InputStream getConfigurationInputStream(String resource)
            throws HibernateException {

        log.info("Configuration resource: " + resource);

        try {
            return FileLocator.openStream(Activator.getDefault().getBundle(),
                    new Path(resource), false);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ConfigHelper.getResourceAsStream(resource);
    }

    public Configuration addResource(String resourceName)
            throws MappingException {
        try {

            return addInputStream(FileLocator.openStream(Activator.getDefault()
                    .getBundle(), new Path(resourceName), false));
        } catch (MappingException me) {
            throw new InvalidMappingException("resource", resourceName, me);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.addResource(resourceName);
    }

 

这样我们的.hbm.xml和.cfg.xml文件就可以放置在插件的任意位置了。

于是就完成了对hibernate的配置。

 

下一步的工作是,把.cfg.xml中数据库的配置部分转移到数据库插件去,实现随时替换数据库。

posted @ 2012-05-23 15:19  荒土  阅读(826)  评论(0编辑  收藏  举报