📄 factorybuilder.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.//// $Id$package org.ozoneDB.tools.OPP.srcgen.builder;import org.ozoneDB.tools.OPP.OPPHelper;import org.ozoneDB.tools.OPP.message.MessageWriter;import org.ozoneDB.tools.OPP.srcgen.ClassBuilder;import org.ozoneDB.tools.OPP.srcgen.BuilderException;import org.ozoneDB.tools.OPP.srcgen.streamfactory.OutputStreamFactory;import java.io.*;import java.lang.reflect.Modifier;/** * Most of the code was lifted out of the FactoryGenerator written by * Leo Mekenkamp. The FactoryGenerator was in turn based on the ProxyGenerator * * The FactoryBuilder is in fact half of the FactoryGenerate, the * generating part. The rest of the FactoryGenerator had to do with * browsing a class for constructors etc. That part is moved to a * director, thus separating concerns. * * @author <a href="http://www.softwarebuero.de/">SMB</a> * @author <a href="http://www.medium.net/">Medium.net</a> * @author Per Nyfelt * @author <a href="mailto://ozone-db.orgATmekenkampD0Tcom">Leo Mekenkamp</a> * @author Joakim Ohlrogge */public class FactoryBuilder implements ClassBuilder { private OutputStreamFactory outputFactory; private String postfix; private PrintWriter out = null; private String[] interfaces; private MessageWriter msgWriter; public FactoryBuilder(OutputStreamFactory outputFactory, String postfix) { this.outputFactory = outputFactory; this.postfix = postfix; } private void makeCreateInternal(String implementationClass, String type) { out.println(" private " + type + " createInternal(OzonePersonalMetaData personalMeta, OzoneSharedMetaData sharedMeta, Class[] ctorArgTypes, Object[] ctorArgs) throws Exception {"); out.println(" String name = null;"); out.println(" int access = OzoneInterface.Public;"); out.println(" if (personalMeta != null) {"); out.println(" name = personalMeta.getName();"); out.println(" }"); out.println(" if (sharedMeta != null) {"); out.println(" access = sharedMeta.getAccess();"); out.println(" }"); out.println(" return (" + type + ") getDatabase().createObject(" + implementationClass + ".class, access, name, ctorArgTypes, ctorArgs);"); out.println(" }\n"); } private void makeGlobalHeader(String implementationClass) { out.println("// Factory class generated by ozone's OPP ($Revision$)."); out.println("// DO NOT EDIT!\n"); if (!OPPHelper.packageName(implementationClass).equals("")) { out.println("package " + OPPHelper.packageName(implementationClass) + ";\n"); } out.println("import org.ozoneDB.AbstractFactory;"); out.println("import org.ozoneDB.OzonePersonalMetaData;"); out.println("import org.ozoneDB.OzoneSharedMetaData;"); out.println("import org.ozoneDB.OzoneInterface;"); out.println("import org.ozoneDB.OzoneInternalException;"); out.println("import org.ozoneDB.OzoneProxy;"); out.println("import org.ozoneDB.tools.OPP.OPP;\n"); } private void makeLocalHeader(String simpleClassName) { out.println("/**"); out.println(" * <p>This class is generated by OPP. DO NOT EDIT, for it will be overwritten"); out.println(" * the next time OPP is run for <code>" + simpleClassName + "</code>.</p>"); out.println(" * <p>Factory pattern class for creating ozone objects.</p>"); out.println(" * A factory has a bit of a schizophrenic nature: on the client-side it 'links'"); out.println(" * to an ExternalDatabase, while on the server-side it does so to the Database"); out.println(" * that holds the instances that the factory creates. Note however that a"); out.println(" * factory running inside an Ozone server can also be linked to an External"); out.println(" * database outside that server (userclient -> server A -> server B). In that"); out.println(" * case such a factory would be 'linked' to an ExternalDatabase.</p>"); out.println(" * <p>The idea behind factories is threefold:<ul>\n"); out.println(" * <li>provide an abstraction from Ozone specific object creation</li>"); out.println(" * <li>facilitate creating objects with non-default constructors</li>"); out.println(" * <li>provide (almost) the same interface on the server side and the client"); out.println(" * side on object creation</li></ul>"); out.println(" * The differences in client side and server side operation are:<ul>"); out.println(" * <li>on the server side <code>getDefault()</code> returns a factory that is"); out.println(" * creates its objects inside that same server database and on the client side"); out.println(" * it returns a factory that creates its objects in the default database</li>"); out.println(" * <li>In order to use <code>getDefault()<code> on the client side, you need to"); out.println(" * call <code>setDefaultDatabaseUrl(String)</code> to specify the default"); out.println(" * database.</li>"); out.println(" * In a typical real-world scenario where you connect to only one ozone database"); out.println(" * you would call <code>setDefaultDatabaseUrl(String)</code> on the client only"); out.println(" * once, and for the rest of the programs lifespan call"); out.println(" * <code>" + simpleClassName + ".getDefault().create()</code> methods both on the server and"); out.println(" * client sides to create objects.</p>"); out.println(" * <p>Note: if you do not have a clue what factories are and how they work, you"); out.println(" * should probably brush up on your knowledge of"); out.println(" * <a href=\"http://www.google.com/search?q=java+design+patterns+GoF\">design patterns</a></p>."); out.println(" * @author <a href=\"mailto://ozone-db.orgATmekenkampD0Tcom\">Leo Mekenkamp</a>"); out.println(" * @since FIXME(since when?)"); out.println(" */\n"); out.println("public final class " + simpleClassName + " extends AbstractFactory {\n"); out.println(" private static class Info implements org.ozoneDB.FactoryClassInfo {"); out.println(" public final void defaultDatabaseUrlChanged() {"); out.println(" defaultInstance = null;"); out.println(" }"); out.println(" }\n"); out.println(" static {"); out.println(" addFactoryClassInfo(new Info());"); out.println(" }\n"); out.println(" private static " + simpleClassName + " defaultInstance = null;\n"); out.println(" /**"); out.println(" * On the client side: returns a factory that is linked to a database"); out.println(" * specified by the url passed to <code>setDefaultDatabaseUrl</code>. On the"); out.println(" * server side: returns a factory that is linked to the server database."); out.println(" * Note that multiple calls to this method return the same value over and"); out.println(" * over again, until <code>setDefaultDatabaseUrl</code> has been called."); out.println(" */"); out.println(" public static synchronized " + simpleClassName + " getDefault() throws Exception {"); out.println(" if (defaultInstance == null) {"); out.println(" defaultInstance = new " + simpleClassName + "();"); out.println(" }"); out.println(" return defaultInstance;"); out.println(" }\n"); // constructors out.println(" /**"); out.println(" * <p>Default constructor: creates a factory that is liked to the default"); out.println(" * database.</p>"); out.println(" */"); out.println(" public " + simpleClassName + "() throws Exception {"); out.println(" }\n"); out.println(" /** <p>Creates a factory that creates its objects in the database specified by"); out.println(" * <code>url</code>. Note that this constructor can only be used if the database in"); out.println(" * question is not opened by this client. Use <code>" + simpleClassName + "()</code> or"); out.println(" * <code>" + simpleClassName + "(Factory)</code> to create a factory for an already opened"); out.println(" * database. This might seem strange, or even annoying at first, but it is very");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -