Source Code Cross Referenced for MMAdmin.java in  » Database-ORM » MMBase » org » mmbase » module » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » MMBase » org.mmbase.module.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:
0003:        This software is OSI Certified Open Source Software.
0004:        OSI Certified is a certification mark of the Open Source Initiative.
0005:
0006:        The license (Mozilla version 1.0) can be read at the MMBase site.
0007:        See http://www.MMBase.org/license
0008:
0009:         */
0010:        package org.mmbase.module.tools;
0011:
0012:        import java.io.File;
0013:        import java.util.*;
0014:        import java.util.Map.Entry;
0015:
0016:        import javax.servlet.http.HttpSession;
0017:
0018:        import org.mmbase.bridge.*;
0019:        import org.mmbase.cache.*;
0020:        import org.mmbase.core.CoreField;
0021:        import org.mmbase.core.util.Fields;
0022:        import org.mmbase.datatypes.DataType;
0023:        import org.mmbase.datatypes.DataTypes;
0024:        import org.mmbase.module.Module;
0025:        import org.mmbase.module.ProcessorModule;
0026:        import org.mmbase.module.builders.Versions;
0027:        import org.mmbase.module.core.*;
0028:        import org.mmbase.model.*;
0029:        import org.mmbase.security.Rank;
0030:        import org.mmbase.storage.StorageException;
0031:        import org.mmbase.storage.search.SearchQueryException;
0032:        import org.mmbase.util.*;
0033:        import org.mmbase.util.functions.*;
0034:        import org.mmbase.util.logging.*;
0035:        import org.mmbase.util.xml.*;
0036:        import org.xml.sax.InputSource;
0037:
0038:        /**
0039:         * @javadoc
0040:         *
0041:         * @application Admin, Application
0042:         * @author Daniel Ockeloen
0043:         * @author Pierre van Rooden
0044:         * @version $Id: MMAdmin.java,v 1.159 2008/02/03 17:33:57 nklasens Exp $
0045:         */
0046:        public class MMAdmin extends ProcessorModule {
0047:            private static final Logger log = Logging
0048:                    .getLoggerInstance(MMAdmin.class);
0049:
0050:            // true: ready (probeCall was called)
0051:            private boolean state = false;
0052:
0053:            /**
0054:             * reference to MMBase
0055:             */
0056:            private MMBase mmb = null;
0057:
0058:            /**
0059:             * @javadoc
0060:             */
0061:            private String lastmsg = "";
0062:
0063:            /**
0064:             * @javadoc
0065:             */
0066:            private boolean kioskmode = false;
0067:
0068:            private static final Parameter<String> PARAM_APPLICATION = new Parameter<String>(
0069:                    "application", String.class);
0070:            private static final Parameter<String> PARAM_BUILDER = new Parameter<String>(
0071:                    "builder", String.class);
0072:            private static final Parameter<String> PARAM_MODULE = new Parameter<String>(
0073:                    "module", String.class);
0074:            private static final Parameter<String> PARAM_FIELD = new Parameter<String>(
0075:                    "field", String.class);
0076:            private static final Parameter<String> PARAM_KEY = new Parameter<String>(
0077:                    "key", String.class);
0078:            private static final Parameter<String> PARAM_CMD = new Parameter<String>(
0079:                    "cmd", String.class);
0080:            private static final Parameter<String> PARAM_PATH = new Parameter<String>(
0081:                    "path", String.class);
0082:            private static final Parameter<?>[] PARAMS_BUILDER = new Parameter<?>[] {
0083:                    PARAM_BUILDER, PARAM_PAGEINFO };
0084:            private static final Parameter<?>[] PARAMS_APPLICATION = new Parameter<?>[] {
0085:                    PARAM_APPLICATION, PARAM_PAGEINFO };
0086:
0087:            {
0088:                addFunction(new GetNodeListFunction("APPLICATIONS",
0089:                        PARAMS_PAGEINFO));
0090:                addFunction(new GetNodeListFunction("BUILDERS", PARAMS_PAGEINFO));
0091:                addFunction(new GetNodeListFunction("MODULES", PARAMS_PAGEINFO));
0092:                addFunction(new GetNodeListFunction("DATABASES",
0093:                        PARAMS_PAGEINFO));
0094:                addFunction(new GetNodeListFunction("MULTILEVELCACHEENTRIES",
0095:                        PARAMS_PAGEINFO));
0096:                addFunction(new GetNodeListFunction("NODECACHEENTRIES",
0097:                        PARAMS_PAGEINFO));
0098:                addFunction(new GetNodeListFunction("FIELDS", PARAMS_BUILDER));
0099:
0100:                addFunction(new ReplaceFunction("VERSION", PARAMS_APPLICATION));
0101:                addFunction(new ReplaceFunction("INSTALLEDVERSION",
0102:                        PARAMS_APPLICATION));
0103:                addFunction(new ReplaceFunction("DESCRIPTION",
0104:                        PARAMS_APPLICATION));
0105:
0106:                addFunction(new ReplaceFunction("BUILDERVERSION",
0107:                        PARAMS_BUILDER));
0108:                addFunction(new ReplaceFunction("BUILDERCLASSFILE",
0109:                        PARAMS_BUILDER));
0110:                addFunction(new ReplaceFunction("BUILDERDESCRIPTION",
0111:                        PARAMS_BUILDER));
0112:
0113:                addFunction(new ReplaceFunction("GETDESCRIPTION",
0114:                        new Parameter[] { PARAM_BUILDER, PARAM_FIELD,
0115:                                Parameter.LANGUAGE, PARAM_PAGEINFO }));
0116:                addFunction(new ReplaceFunction("GETGUINAMEVALUE",
0117:                        new Parameter[] { PARAM_BUILDER, PARAM_FIELD,
0118:                                Parameter.LANGUAGE, PARAM_PAGEINFO }));
0119:                addFunction(new ReplaceFunction("GETBUILDERFIELD",
0120:                        new Parameter[] { PARAM_BUILDER, PARAM_FIELD,
0121:                                PARAM_KEY, PARAM_PAGEINFO }));
0122:                addFunction(new ReplaceFunction("GETMODULEPROPERTY",
0123:                        new Parameter[] { PARAM_MODULE, PARAM_KEY,
0124:                                PARAM_PAGEINFO }));
0125:                addFunction(new ReplaceFunction("MODULEDESCRIPTION",
0126:                        new Parameter[] { PARAM_MODULE, PARAM_PAGEINFO }));
0127:                addFunction(new ReplaceFunction("MODULECLASSFILE",
0128:                        new Parameter[] { PARAM_MODULE, PARAM_PAGEINFO }));
0129:
0130:                addFunction(new ReplaceFunction("MULTILEVELCACHEHITS",
0131:                        PARAMS_PAGEINFO));
0132:                addFunction(new ReplaceFunction("MULTILEVELCACHEMISSES",
0133:                        PARAMS_PAGEINFO));
0134:                addFunction(new ReplaceFunction("MULTILEVELCACHEREQUESTS",
0135:                        PARAMS_PAGEINFO));
0136:                addFunction(new ReplaceFunction("MULTILEVELCACHEPERFORMANCE",
0137:                        PARAMS_PAGEINFO));
0138:                addFunction(new ReplaceFunction("MULTILEVELCACHESTATE",
0139:                        new Parameter<?>[] {
0140:                                new Parameter<String>("state", String.class),
0141:                                PARAM_PAGEINFO }));
0142:                addFunction(new ReplaceFunction("MULTILEVELCACHESIZE",
0143:                        PARAMS_PAGEINFO));
0144:
0145:                addFunction(new ReplaceFunction("NODECACHEHITS",
0146:                        PARAMS_PAGEINFO));
0147:                addFunction(new ReplaceFunction("NODECACHEMISSES",
0148:                        PARAMS_PAGEINFO));
0149:                addFunction(new ReplaceFunction("NODECACHEREQUESTS",
0150:                        PARAMS_PAGEINFO));
0151:                addFunction(new ReplaceFunction("NODECACHEPERFORMANCE",
0152:                        PARAMS_PAGEINFO));
0153:                addFunction(new ReplaceFunction("NODECACHESIZE",
0154:                        PARAMS_PAGEINFO));
0155:                addFunction(new ReplaceFunction("TEMPORARYNODECACHESIZE",
0156:                        PARAMS_PAGEINFO));
0157:                addFunction(new ReplaceFunction("RELATIONCACHEHITS",
0158:                        PARAMS_PAGEINFO));
0159:                addFunction(new ReplaceFunction("RELATIONCACHEMISSES",
0160:                        PARAMS_PAGEINFO));
0161:                addFunction(new ReplaceFunction("RELATIONCACHEREQUESTS",
0162:                        PARAMS_PAGEINFO));
0163:                addFunction(new ReplaceFunction("RELATIONCACHEPERFORMANCE",
0164:                        PARAMS_PAGEINFO));
0165:
0166:                addFunction(new ProcessFunction("LOAD", new Parameter[] {
0167:                        PARAM_APPLICATION, PARAM_PAGEINFO,
0168:                        new Parameter("RESULT", String.class, "") }));
0169:                addFunction(new ProcessFunction("SAVE", new Parameter[] {
0170:                        PARAM_APPLICATION, PARAM_PATH, PARAM_PAGEINFO,
0171:                        new Parameter("RESULT", String.class, "") }));
0172:                addFunction(new ProcessFunction("BUILDERSAVE", new Parameter[] {
0173:                        PARAM_BUILDER, PARAM_PATH, PARAM_PAGEINFO,
0174:                        new Parameter("RESULT", String.class, "") }));
0175:            }
0176:
0177:            public MMAdmin(String name) {
0178:                super (name);
0179:            }
0180:
0181:            /**
0182:             * @javadoc
0183:             */
0184:            public void init() {
0185:                String dtmp = System.getProperty("mmbase.kiosk");
0186:                if (dtmp != null && dtmp.equals("yes")) {
0187:                    kioskmode = true;
0188:                    log.info("*** Server started in kiosk mode ***");
0189:                }
0190:                mmb = MMBase.getMMBase();
0191:
0192:                new MMAdminProbe(this , mmb);
0193:            }
0194:
0195:            /**
0196:             * Returns a virtual builder used to create node lists from the results
0197:             * returned by getList().
0198:             * The default method does not associate the builder with a cloud (mmbase module),
0199:             * so processormodules that need this association need to override this method.
0200:             * Note that different lists may return different builders.
0201:             * @param command the LIST command for which to retrieve the builder
0202:             * @param params contains the attributes for the list
0203:             */
0204:            public MMObjectBuilder getListBuilder(String command,
0205:                    Map<String, Object> params) {
0206:                return new VirtualBuilder(mmb);
0207:            }
0208:
0209:            /**
0210:             * Retrieves a specified builder.
0211:             * The builder's name can be extended with the subpath of that builder's configuration file.
0212:             * i.e. 'core/typedef' or 'basic/images'. The subpath part is ignored.
0213:             * @param path The path of the builder to retrieve
0214:             * @return a <code>MMObjectBuilder</code> is found, <code>null</code> otherwise
0215:             */
0216:            public MMObjectBuilder getBuilder(String path) {
0217:                int pos = path.lastIndexOf(File.separator);
0218:                if (pos != -1) {
0219:                    path = path.substring(pos + 1);
0220:                }
0221:                return mmb.getBuilder(path);
0222:            }
0223:
0224:            /**
0225:             * Generate a list of values from a command to the processor
0226:             * @javadoc
0227:             */
0228:            public Vector<String> getList(PageInfo sp, StringTagger tagger,
0229:                    String value) {
0230:                String line = Strip.DoubleQuote(value, Strip.BOTH);
0231:                StringTokenizer tok = new StringTokenizer(line, "-\n\r");
0232:                if (tok.hasMoreTokens()) {
0233:                    String cmd = tok.nextToken();
0234:                    log.debug("Cmd '" + cmd + "'");
0235:                    if (!checkUserLoggedOn(sp, cmd, false)) {
0236:                        log.warn("Could not find cloud for " + sp
0237:                                + " returning empty list for " + tagger + "/"
0238:                                + value);
0239:                        return new Vector<String>();
0240:                    }
0241:                    if (cmd.equals("APPLICATIONS")) {
0242:                        tagger.setValue("ITEMS", "5");
0243:                        try {
0244:                            return getApplicationsList();
0245:                        } catch (SearchQueryException e) {
0246:                            log.warn(Logging.stackTrace(e));
0247:                        }
0248:                    }
0249:                    if (cmd.equals("BUILDERS")) {
0250:                        tagger.setValue("ITEMS", "4");
0251:                        if ((tok != null) && (tok.hasMoreTokens())) {
0252:                            tok.nextToken();
0253:                        }
0254:                        return getBuildersList();
0255:                    }
0256:                    if (cmd.equals("FIELDS")) {
0257:                        tagger.setValue("ITEMS", "4");
0258:                        return getFields(tok.nextToken());
0259:                    }
0260:                    if (cmd.equals("MODULEPROPERTIES")) {
0261:                        tagger.setValue("ITEMS", "2");
0262:                        return getModuleProperties(tok.nextToken());
0263:                    }
0264:                    if (cmd.equals("ISOGUINAMES")) {
0265:                        tagger.setValue("ITEMS", "2");
0266:                        return getISOGuiNames(tok.nextToken(), tok.nextToken());
0267:                    }
0268:                    if (cmd.equals("ISODESCRIPTIONS")) {
0269:                        tagger.setValue("ITEMS", "2");
0270:                        return getISODescriptions(tok.nextToken(), tok
0271:                                .nextToken());
0272:                    }
0273:                    if (cmd.equals("MODULES")) {
0274:                        tagger.setValue("ITEMS", "4");
0275:                        return getModulesList();
0276:                    }
0277:                    if (cmd.equals("DATABASES")) {
0278:                        tagger.setValue("ITEMS", "4");
0279:                        return getDatabasesList();
0280:                    }
0281:                    if (cmd.equals("MULTILEVELCACHEENTRIES")) {
0282:                        tagger.setValue("ITEMS", "8");
0283:                        return getMultilevelCacheEntries();
0284:                    }
0285:                    if (cmd.equals("NODECACHEENTRIES")) {
0286:                        tagger.setValue("ITEMS", "4");
0287:                        return getNodeCacheEntries();
0288:                    }
0289:                }
0290:                return null;
0291:            }
0292:
0293:            /**
0294:             * @javadoc
0295:             */
0296:            private boolean checkAdmin(PageInfo sp, String cmd) {
0297:                return checkUserLoggedOn(sp, cmd, true);
0298:            }
0299:
0300:            /**
0301:             * @javadoc
0302:             */
0303:            private boolean checkUserLoggedOn(PageInfo sp, String cmd,
0304:                    boolean adminonly) {
0305:
0306:                if (sp.getCloud() != null) {
0307:                    if ((!adminonly)
0308:                            || sp.getCloud().getUser().getRank().getInt() >= Rank.ADMIN
0309:                                    .getInt()) {
0310:                        log.debug("Found cloud " + sp.getCloud().getUser());
0311:                        return true;
0312:                    }
0313:                }
0314:                // check if the we are using jsp, and logged on as user with rank is admin, this means that
0315:                // there is some user with rank Administrator in the session...
0316:
0317:                HttpSession session = sp.req.getSession(false);
0318:                Enumeration e = session.getAttributeNames();
0319:                while (e.hasMoreElements()) {
0320:                    String attribute = (String) e.nextElement();
0321:                    Object o = session.getAttribute(attribute);
0322:
0323:                    if (o instanceof  Cloud) {
0324:                        Cloud cloud = (Cloud) o;
0325:                        Rank curRank = cloud.getUser().getRank();
0326:                        if (curRank.getInt() >= Rank.ADMIN.getInt()) {
0327:                            // log.service("Found an administrator cloud in session key=" + attribute);
0328:                            return true;
0329:                        }
0330:                    }
0331:                }
0332:
0333:                return false;
0334:            }
0335:
0336:            /**
0337:             * Execute the commands provided in the form values
0338:             * @javadoc
0339:             */
0340:            public boolean process(PageInfo sp, Hashtable<String, Object> cmds,
0341:                    Hashtable<String, Object> vars) {
0342:                String cmdline, token;
0343:                for (Enumeration<String> h = cmds.keys(); h.hasMoreElements();) {
0344:                    cmdline = h.nextElement();
0345:                    log.debug("cmdline: " + cmdline);
0346:                    if (!checkAdmin(sp, cmdline)) {
0347:                        log.warn("Could not find cloud for " + sp
0348:                                + " returning false for process " + cmds + "/"
0349:                                + vars);
0350:                        return false;
0351:                    }
0352:                    StringTokenizer tok = new StringTokenizer(cmdline, "-\n\r");
0353:                    token = tok.nextToken();
0354:                    if (token.equals("SERVERRESTART")) {
0355:                        lastmsg = "Server restart is not implemented any more";
0356:                        return false;
0357:                    } else if (token.equals("LOAD") && !kioskmode) {
0358:                        ApplicationResult result = new ApplicationResult();
0359:                        String appname = (String) cmds.get(cmdline);
0360:                        if ("".equals(appname)) {
0361:                            log.warn("Found empty app-name in " + cmds
0362:                                    + " (used key " + cmdline + ")");
0363:                        }
0364:                        try {
0365:                            if (new ApplicationInstaller(mmb, this )
0366:                                    .installApplication(appname, -1, null,
0367:                                            result, new HashSet<String>(),
0368:                                            false)) {
0369:                                lastmsg = result.getMessage();
0370:                            } else {
0371:                                lastmsg = "Problem installing application : "
0372:                                        + appname + ", cause: "
0373:                                        + result.getMessage();
0374:                            }
0375:                        } catch (SearchQueryException e) {
0376:                            log.warn(Logging.stackTrace(e));
0377:                        }
0378:                        if (vars != null) {
0379:                            vars.put("RESULT", lastmsg);
0380:                        }
0381:                    } else if (token.equals("SAVE")) {
0382:                        String appname = (String) cmds.get(cmdline);
0383:                        String savepath = (String) vars.get("path");
0384:                        String goal = (String) vars.get("goal");
0385:                        boolean includeComments = true;
0386:                        /* if (tok.hasMoreTokens()) {
0387:                            includeComments = "true".equals(tok.nextToken());
0388:                        } */
0389:                        writeApplication(appname, savepath, goal,
0390:                                includeComments);
0391:                        if (vars != null) {
0392:                            vars.put("RESULT", lastmsg);
0393:                        }
0394:                    } else if (token.equals("BUILDER")) {
0395:                        doBuilderPosts(tok.nextToken(), cmds, vars);
0396:                    } else if (token.equals("MODULE")) {
0397:                        doModulePosts(tok.nextToken(), cmds, vars);
0398:                    } else if (token.equals("MODULESAVE")) {
0399:                        if (kioskmode) {
0400:                            log
0401:                                    .warn("MMAdmin> refused to write module, am in kiosk mode");
0402:                        } else {
0403:                            String modulename = (String) cmds.get(cmdline);
0404:                            String savepath = (String) vars.get("PATH");
0405:                            Module mod = getModule(modulename);
0406:                            if (mod != null) {
0407:                                try {
0408:                                    boolean includeComments = false;
0409:                                    if (tok.hasMoreTokens()) {
0410:                                        includeComments = "true".equals(tok
0411:                                                .nextToken());
0412:                                    }
0413:                                    ModuleWriter moduleOut = new ModuleWriter(
0414:                                            mod);
0415:                                    moduleOut
0416:                                            .setIncludeComments(includeComments);
0417:                                    moduleOut.writeToFile(savepath);
0418:                                } catch (Exception e) {
0419:                                    log.error(Logging.stackTrace(e));
0420:                                    lastmsg = "Writing finished, problems occurred\n\nError encountered="
0421:                                            + e.getMessage() + "\n\n";
0422:                                    return false;
0423:                                }
0424:                                lastmsg = "Writing finished, no problems.\n\nA clean copy of "
0425:                                        + modulename
0426:                                        + ".xml can be found at : "
0427:                                        + savepath
0428:                                        + "\n\n";
0429:                            } else {
0430:                                lastmsg = "Writing failed, module : "
0431:                                        + modulename
0432:                                        + ".xml because module is not loaded\n\n";
0433:                                return false;
0434:                            }
0435:                        }
0436:                    } else if (token.equals("BUILDERSAVE")) {
0437:                        if (kioskmode) {
0438:                            log
0439:                                    .warn("MMAdmin> refused to write builder, am in kiosk mode");
0440:                        } else {
0441:                            String buildername = (String) cmds.get(cmdline);
0442:                            String savepath = (String) vars.get("path");
0443:                            MMObjectBuilder bul = getBuilder(buildername);
0444:                            if (bul != null) {
0445:                                try {
0446:                                    boolean includeComments = false;
0447:                                    if (tok.hasMoreTokens()) {
0448:                                        includeComments = "true".equals(tok
0449:                                                .nextToken());
0450:                                    }
0451:                                    BuilderWriter builderOut = new BuilderWriter(
0452:                                            bul);
0453:                                    builderOut
0454:                                            .setIncludeComments(includeComments);
0455:                                    builderOut.setExpandBuilder(false);
0456:                                    builderOut.writeToFile(savepath);
0457:                                    lastmsg = "Writing finished, no problems.\n\nA clean copy of "
0458:                                            + buildername
0459:                                            + ".xml can be found at : "
0460:                                            + savepath + "\n\n";
0461:                                } catch (Exception e) {
0462:                                    log.error(Logging.stackTrace(e));
0463:                                    lastmsg = "Writing finished, problems occurred\n\n"
0464:                                            + "Error encountered="
0465:                                            + e.getMessage() + "\n\n";
0466:                                }
0467:                                if (vars != null) {
0468:                                    vars.put("RESULT", lastmsg);
0469:                                }
0470:                            }
0471:                        }
0472:                    }
0473:                }
0474:                return false;
0475:            }
0476:
0477:            // basically replaces linefeeds and some characters.
0478:            private String escape(String s) {
0479:                if (s == null) {
0480:                    return "";
0481:                } else {
0482:                    Encode encoder = new Encode("ESCAPE_XML");
0483:                    String escaped = encoder.encode(s);
0484:                    return escaped.replaceAll("\n", "<br />");
0485:                }
0486:            }
0487:
0488:            /**
0489:             * @since MMBase-1.8.5
0490:             */
0491:            protected Collection<String> getIgnoredAutodeployApplications() {
0492:                return Casting
0493:                        .toCollection(getInitParameter("ignored-auto-deploy"));
0494:            }
0495:
0496:            /**
0497:             * Handle a $MOD command
0498:             * @javadoc
0499:             */
0500:            public String replace(PageInfo sp, String cmds) {
0501:                if (!checkUserLoggedOn(sp, cmds, false))
0502:                    return "";
0503:                StringTokenizer tok = new StringTokenizer(cmds, "-\n\r");
0504:                if (tok.hasMoreTokens()) {
0505:                    String cmd = tok.nextToken();
0506:                    if (cmd.equals("VERSION")) {
0507:                        return "" + getVersion(tok.nextToken());
0508:                    } else if (cmd.equals("INSTALLEDVERSION")) {
0509:                        Versions ver = (Versions) mmb.getBuilder("versions");
0510:                        if (ver == null) {
0511:                            log
0512:                                    .warn("Versions builder not installed, Can't get to apps");
0513:                            return null;
0514:                        }
0515:                        return ""
0516:                                + ver.getInstalledVersion(tok.nextToken(),
0517:                                        "application");
0518:                    } else if (cmd.equals("DESCRIPTION")) {
0519:                        return escape(getDescription(tok.nextToken()));
0520:                    } else if (cmd.equals("LASTMSG")) {
0521:                        // return lastmsg in html-format.
0522:                        return escape(lastmsg);
0523:                    } else if (cmd.equals("BUILDERVERSION")) {
0524:                        return "" + getBuilderVersion(tok.nextToken());
0525:                    } else if (cmd.equals("BUILDERCLASSFILE")) {
0526:                        return "" + getBuilderClass(tok.nextToken());
0527:                    } else if (cmd.equals("BUILDERDESCRIPTION")) {
0528:                        return "" + getBuilderDescription(tok.nextToken());
0529:                    } else if (cmd.equals("GETDESCRIPTION")) {
0530:                        return getDescription(tok.nextToken(), tok.nextToken(),
0531:                                tok.nextToken());
0532:                    } else if (cmd.equals("GETGUINAMEVALUE")) {
0533:                        return getGuiNameValue(tok.nextToken(),
0534:                                tok.nextToken(), tok.nextToken());
0535:                    } else if (cmd.equals("GETBUILDERFIELD")) {
0536:                        return getBuilderField(tok.nextToken(),
0537:                                tok.nextToken(), tok.nextToken());
0538:                    } else if (cmd.equals("GETMODULEPROPERTY")) {
0539:                        return getModuleProperty(tok.nextToken(), tok
0540:                                .nextToken());
0541:                    } else if (cmd.equals("MODULEDESCRIPTION")) {
0542:                        return "" + getModuleDescription(tok.nextToken());
0543:                    } else if (cmd.equals("MODULECLASSFILE")) {
0544:                        return "" + getModuleClass(tok.nextToken());
0545:                    } else if (cmd.equals("MULTILEVELCACHEHITS")) {
0546:                        return ("" + MultilevelCache.getCache().getHits());
0547:                    } else if (cmd.equals("MULTILEVELCACHEMISSES")) {
0548:                        return ("" + MultilevelCache.getCache().getMisses());
0549:                    } else if (cmd.equals("MULTILEVELCACHEREQUESTS")) {
0550:                        return ("" + (MultilevelCache.getCache().getHits() + MultilevelCache
0551:                                .getCache().getMisses()));
0552:                    } else if (cmd.equals("MULTILEVELCACHEPERFORMANCE")) {
0553:                        return ("" + (MultilevelCache.getCache().getRatio() * 100));
0554:                    } else if (cmd.equals("MULTILEVELCACHESTATE")) {
0555:                        if (tok.hasMoreTokens()) {
0556:                            String state = tok.nextToken();
0557:                            if (state.equalsIgnoreCase("On")) {
0558:                                MultilevelCache.getCache().setActive(true);
0559:                                log.info("turned multilevelcache on");
0560:                            } else if (state.equalsIgnoreCase("Off")) {
0561:                                MultilevelCache.getCache().setActive(false);
0562:                                log.info("turned multilevelcache off");
0563:                            }
0564:                        } else {
0565:                            if (MultilevelCache.getCache().isActive()) {
0566:                                return "On";
0567:                            } else {
0568:                                return "Off";
0569:                            }
0570:                        }
0571:                    } else if (cmd.equals("MULTILEVELCACHESIZE")) {
0572:                        return ("" + (MultilevelCache.getCache().maxSize()));
0573:                    } else if (cmd.equals("NODECACHEHITS")) {
0574:                        return ("" + NodeCache.getCache().getHits());
0575:                    } else if (cmd.equals("NODECACHEMISSES")) {
0576:                        return ("" + NodeCache.getCache().getMisses());
0577:                    } else if (cmd.equals("NODECACHEREQUESTS")) {
0578:                        return ("" + (NodeCache.getCache().getHits() + NodeCache
0579:                                .getCache().getMisses()));
0580:                    } else if (cmd.equals("NODECACHEPERFORMANCE")) {
0581:                        return ("" + (NodeCache.getCache().getRatio() * 100));
0582:                    } else if (cmd.equals("NODECACHESIZE")) {
0583:                        return ("" + (NodeCache.getCache().maxSize()));
0584:                    } else if (cmd.equals("TEMPORARYNODECACHESIZE")) {
0585:                        return ("" + (MMObjectBuilder.temporaryNodes.size()));
0586:                    } else if (cmd.equals("RELATIONCACHEHITS")) {
0587:                        return ("" + RelationsCache.getCache().getHits());
0588:                    } else if (cmd.equals("RELATIONCACHEMISSES")) {
0589:                        return ("" + RelationsCache.getCache().getMisses());
0590:                    } else if (cmd.equals("RELATIONCACHEREQUESTS")) {
0591:                        return ("" + (RelationsCache.getCache().getHits() + RelationsCache
0592:                                .getCache().getMisses()));
0593:                    } else if (cmd.equals("RELATIONCACHEPERFORMANCE")) {
0594:                        return ""
0595:                                + (1.0 * RelationsCache.getCache().getHits())
0596:                                / (RelationsCache.getCache().getHits()
0597:                                        + RelationsCache.getCache().getMisses() + 0.0000000001)
0598:                                * 100;
0599:                    }
0600:                }
0601:                return "No command defined";
0602:            }
0603:
0604:            /**
0605:             * @javadoc
0606:             */
0607:            int getVersion(String appname) {
0608:                ApplicationReader reader = getApplicationReader(appname);
0609:                if (reader != null) {
0610:                    return reader.getVersion();
0611:                }
0612:                return -1;
0613:            }
0614:
0615:            // determine xmlpath to a builder, provided it is loaded by MMBase.
0616:            private String getXMLPath(String builderName) {
0617:                MMObjectBuilder bul = mmb.getBuilder(builderName);
0618:                if (bul == null) {
0619:                    return "";
0620:                } else {
0621:                    return bul.getXMLPath();
0622:                }
0623:            }
0624:
0625:            /**
0626:             * @javadoc
0627:             */
0628:            int getBuilderVersion(String builderName) {
0629:                int version = -1;
0630:                BuilderReader bul = mmb
0631:                        .getBuilderReader(getXMLPath(builderName) + builderName);
0632:                if (bul != null) {
0633:                    version = bul.getVersion();
0634:                }
0635:                return version;
0636:            }
0637:
0638:            /**
0639:             * @javadoc
0640:             */
0641:            String getBuilderClass(String builderName) {
0642:                String className = "";
0643:                BuilderReader bul = mmb
0644:                        .getBuilderReader(getXMLPath(builderName) + builderName);
0645:                if (bul != null) {
0646:                    className = bul.getClassName();
0647:                }
0648:                return className;
0649:            }
0650:
0651:            /**
0652:             * @javadoc
0653:             */
0654:            String getModuleClass(String modname) {
0655:                String className = "";
0656:                ModuleReader mod = getModuleReader(modname);
0657:                if (mod != null) {
0658:                    className = mod.getClassName();
0659:                }
0660:                return className;
0661:            }
0662:
0663:            /**
0664:             * @javadoc
0665:             */
0666:            public void setModuleProperty(Hashtable<String, Object> vars) {
0667:                if (kioskmode) {
0668:                    log.warn("refused module property set, am in kiosk mode");
0669:                    return;
0670:                }
0671:                String modname = (String) vars.get("MODULE");
0672:                String key = (String) vars.get("PROPERTYNAME");
0673:                String value = (String) vars.get("VALUE");
0674:                Module mod = getModule(modname);
0675:                log.debug("MOD=" + mod);
0676:                if (mod != null) {
0677:                    mod.setInitParameter(key, value);
0678:                    syncModuleXML(mod, modname);
0679:                }
0680:
0681:            }
0682:
0683:            /**
0684:             * @javadoc
0685:             * @todo should obtain data from the configuration file
0686:             */
0687:            String getModuleProperty(String modname, String key) {
0688:                Module mod = getModule(modname);
0689:                if (mod != null) {
0690:                    String value = mod.getInitParameter(key);
0691:                    if (value != null) {
0692:                        return value;
0693:                    }
0694:                }
0695:                return "";
0696:
0697:            }
0698:
0699:            /**
0700:             * @javadoc
0701:             */
0702:            String getDescription(String appname) {
0703:                ApplicationReader reader = getApplicationReader(appname);
0704:                if (reader != null) {
0705:                    return reader.getDescription();
0706:                }
0707:                return "";
0708:
0709:            }
0710:
0711:            /**
0712:             * @javadoc
0713:             */
0714:            String getBuilderDescription(String builderName) {
0715:                String description = "";
0716:                BuilderReader bul = mmb
0717:                        .getBuilderReader(getXMLPath(builderName) + builderName);
0718:                if (bul != null) {
0719:                    Map<String, String> desc = bul.getDescriptions();
0720:                    String english = desc.get("en");
0721:                    if (english != null) {
0722:                        description = english;
0723:                    }
0724:                }
0725:                return description;
0726:            }
0727:
0728:            /**
0729:             * @javadoc
0730:             */
0731:            String getModuleDescription(String modulename) {
0732:                Module mod = getModule(modulename);
0733:                if (mod != null) {
0734:                    String value = mod.getModuleInfo();
0735:                    if (value != null)
0736:                        return value;
0737:                }
0738:                return "";
0739:            }
0740:
0741:            /**
0742:             * @javadoc
0743:             */
0744:            public void probeCall() throws SearchQueryException {
0745:                Versions ver = (Versions) mmb.getBuilder("versions");
0746:                if (ver == null) {
0747:                    log
0748:                            .warn("Versions builder not installed, Can't auto deploy apps");
0749:                    return;
0750:                }
0751:                ApplicationInstaller installer = new ApplicationInstaller(mmb,
0752:                        this );
0753:
0754:                installer.installApplications();
0755:                state = true;
0756:            }
0757:
0758:            /**
0759:             * Wether MMAdmin module was completely initialized (applications auto-deployed and so on).
0760:             * @since MMBase-1.7
0761:             */
0762:
0763:            public boolean getState() {
0764:                return state;
0765:            }
0766:
0767:            /**
0768:             * @javadoc
0769:             */
0770:            private boolean writeApplication(String name, String targetPath,
0771:                    String goal, boolean includeComments) {
0772:                if (kioskmode) {
0773:                    log.warn("refused to write application, am in kiosk mode");
0774:                    return false;
0775:                }
0776:                ApplicationReader reader = getApplicationReader(name);
0777:                ApplicationWriter writer = new ApplicationWriter(reader, mmb);
0778:                writer.setIncludeComments(includeComments);
0779:                java.io.Writer w = new java.io.StringWriter();
0780:                Logger logger = new WriterLogger(w);
0781:                try {
0782:                    writer.writeToPath(targetPath, logger);
0783:                    lastmsg = "Application saved oke\n\n";
0784:                } catch (Exception e) {
0785:                    lastmsg = "Saving application failed\n\n"
0786:                            + Logging.stackTrace(e) + "\n\n";
0787:                }
0788:                lastmsg += "Some statistics on the save : \n\n" + w.toString();
0789:                return true;
0790:            }
0791:
0792:            /**
0793:             * This method uses the {@link ResourceLoader} to fetch an application by name. for this purpose
0794:             * it requests the resource by adding <code>applications/</code> to the start of the appName and appends <code>.xml</core> to the end
0795:             * @param appName the name of the application to be read.
0796:             * @return the ApplicationReader for the application, or null is the application wat not found or an exception occured. In the later a message is logged
0797:             */
0798:            private ApplicationReader getApplicationReader(String appName) {
0799:                String resourceName = appName + ".xml";
0800:                try {
0801:                    ResourceLoader applicationLoader = ResourceLoader
0802:                            .getConfigurationRoot().getChildResourceLoader(
0803:                                    "applications");
0804:                    InputSource is = applicationLoader
0805:                            .getInputSource(resourceName);
0806:                    if (is == null) {
0807:                        return null;
0808:                    }
0809:                    return new ApplicationReader(is);
0810:                } catch (Exception e) {
0811:                    log.error("error while reading application from resource "
0812:                            + resourceName + " : " + e.getMessage(), e);
0813:                    return null;
0814:                }
0815:            }
0816:
0817:            /**
0818:             * @javadoc
0819:             */
0820:            Vector<String> getApplicationsList() throws SearchQueryException {
0821:                Vector<String> results = new Vector<String>(); //sigh, synchronized, for what?
0822:                if (mmb == null) {
0823:                    log.warn("MMBase not yet initialized, Can't get to apps");
0824:                    return results;
0825:                }
0826:                Versions ver = (Versions) mmb.getBuilder("versions");
0827:                if (ver == null) {
0828:                    log
0829:                            .warn("Versions builder not installed, Can't get to apps");
0830:                    return results;
0831:                }
0832:
0833:                ResourceLoader applicationLoader = ResourceLoader
0834:                        .getConfigurationRoot().getChildResourceLoader(
0835:                                "applications");
0836:                Iterator<String> i = applicationLoader.getResourcePaths(
0837:                        ResourceLoader.XML_PATTERN, false).iterator();
0838:                while (i.hasNext()) {
0839:                    String appResource = i.next();
0840:                    log.debug("application " + appResource);
0841:                    ApplicationReader reader;
0842:                    try {
0843:                        reader = new ApplicationReader(applicationLoader
0844:                                .getInputSource(appResource));
0845:                    } catch (Exception e) {
0846:                        log.error(e);
0847:                        continue;
0848:                    }
0849:
0850:                    String name = reader.getName();
0851:                    results.add(name);
0852:                    results.add("" + reader.getVersion());
0853:                    int installedversion = ver.getInstalledVersion(name,
0854:                            "application");
0855:                    if (installedversion == -1) {
0856:                        results.add("no");
0857:                    } else {
0858:                        results.add("yes (ver : " + installedversion + ")");
0859:                    }
0860:                    results.add(reader.getMaintainer());
0861:                    boolean autodeploy = reader.hasAutoDeploy();
0862:                    if (autodeploy) {
0863:                        results.add("yes");
0864:                    } else {
0865:                        results.add("no");
0866:                    }
0867:                }
0868:                return results;
0869:            }
0870:
0871:            /**
0872:             * @javadoc
0873:             */
0874:            Vector<String> getBuildersList() {
0875:                Versions ver = (Versions) mmb.getBuilder("versions");
0876:                Vector<String> results = new Vector<String>();
0877:                ResourceLoader builderLoader = mmb.getBuilderLoader();
0878:                Iterator<String> builders = builderLoader.getResourcePaths(
0879:                        ResourceLoader.XML_PATTERN, true).iterator();
0880:                while (builders.hasNext()) {
0881:                    String builderResource = builders.next();
0882:                    String builderName = ResourceLoader
0883:                            .getName(builderResource);
0884:                    BuilderReader reader = mmb
0885:                            .getBuilderReader(getXMLPath(builderName)
0886:                                    + builderName);
0887:                    if (reader == null) {
0888:                        log.error("Did not find reader for " + builderResource);
0889:                        continue;
0890:                    }
0891:                    results.add(builderName);
0892:                    results.add("" + reader.getVersion());
0893:                    int installedversion = -1;
0894:                    if (ver != null) {
0895:                        installedversion = ver.getInstalledVersion(builderName,
0896:                                "builder");
0897:                    }
0898:                    if (installedversion == -1) {
0899:                        results.add("no");
0900:                    } else {
0901:                        results.add("yes");
0902:                    }
0903:                    results.add(reader.getMaintainer());
0904:                }
0905:                return results;
0906:            }
0907:
0908:            /**
0909:             * @javadoc
0910:             */
0911:            Vector<String> getModuleProperties(String modulename) {
0912:                Vector<String> results = new Vector<String>();
0913:                ModuleReader mod = getModuleReader(modulename);
0914:                if (mod != null) {
0915:                    Map<String, String> props = mod.getProperties();
0916:                    for (String key : props.keySet()) {
0917:                        String value = props.get(key);
0918:                        results.add(key);
0919:                        results.add(value);
0920:                    }
0921:                }
0922:                return results;
0923:            }
0924:
0925:            /**
0926:             * @javadoc
0927:             */
0928:            Vector<String> getFields(String builderName) {
0929:                Vector<String> results = new Vector<String>();
0930:                BuilderReader bul = mmb
0931:                        .getBuilderReader(getXMLPath(builderName) + builderName);
0932:                if (bul != null) {
0933:                    List<CoreField> defs = bul.getFields();
0934:                    for (CoreField def : defs) {
0935:                        results.add("" + def.getStoragePosition());
0936:                        results.add("" + def.getName());
0937:                        results.add(Fields.getTypeDescription(def.getType()));
0938:                        int size = def.getMaxLength();
0939:                        if (size == -1) {
0940:                            results.add("fixed");
0941:                        } else {
0942:                            results.add("" + size);
0943:                        }
0944:                    }
0945:                }
0946:                return results;
0947:            }
0948:
0949:            /**
0950:             * @javadoc
0951:             */
0952:            Vector<String> getModulesList() {
0953:                Vector<String> results = new Vector<String>();
0954:                ResourceLoader moduleLoader = getModuleLoader();
0955:                // new code checks all the *.xml files in modules dir
0956:                for (String path : moduleLoader.getResourcePaths(
0957:                        ResourceLoader.XML_PATTERN, false)) {
0958:                    ;
0959:                    String sname = ResourceLoader.getName(path);
0960:                    ModuleReader reader = getModuleReader(sname);
0961:                    if (reader == null) {
0962:                        log.error("Could not load module with xml '" + path
0963:                                + "'");
0964:                        continue;
0965:                    }
0966:                    results.add(sname);
0967:                    results.add("" + reader.getVersion());
0968:                    String status = reader.getStatus();
0969:                    if (status.equals("active")) {
0970:                        results.add("yes");
0971:                    } else {
0972:                        results.add("no");
0973:                    }
0974:                    results.add(reader.getMaintainer());
0975:                }
0976:                return results;
0977:            }
0978:
0979:            /**
0980:             * @javadoc
0981:             */
0982:            Vector<String> getDatabasesList() {
0983:                Versions ver = (Versions) mmb.getBuilder("versions");
0984:                if (ver == null) {
0985:                    log
0986:                            .warn("Versions builder not installed, Can't get to builders");
0987:                    return null;
0988:                }
0989:                Vector<String> results = new Vector<String>();
0990:
0991:                String path = MMBaseContext.getConfigPath() + File.separator
0992:                        + "databases" + File.separator;
0993:                // new code checks all the *.xml files in builder dir
0994:                File bdir = new File(path);
0995:                if (bdir.isDirectory()) {
0996:                    String files[] = bdir.list();
0997:                    if (files == null)
0998:                        return results;
0999:                    for (String aname : files) {
1000:                        if (aname.endsWith(".xml")) {
1001:                            String name = aname;
1002:                            String sname = name.substring(0, name.length()
1003:                                    - ".xml".length());
1004:                            results.add(sname);
1005:
1006:                            results.add("0");
1007:                            results.add("yes");
1008:                            results.add("mmbase.org");
1009:                        }
1010:                    }
1011:                }
1012:                return results;
1013:            }
1014:
1015:            /**
1016:             * @javadoc
1017:             */
1018:            private String getBuilderField(String builderName,
1019:                    String fieldName, String key) {
1020:                MMObjectBuilder bul = getBuilder(builderName);
1021:                if (bul != null) {
1022:                    CoreField def = bul.getField(fieldName);
1023:                    if (def == null)
1024:                        throw new RuntimeException("No such field '"
1025:                                + fieldName + "' in builder '" + builderName
1026:                                + "'");
1027:                    if (key.equals("dbkey")) {
1028:                        if (def.isUnique()) {
1029:                            return "true";
1030:                        } else {
1031:                            return "false";
1032:                        }
1033:                    } else if (key.equals("dbnotnull")) {
1034:                        if (def.isRequired()) {
1035:                            return "true";
1036:                        } else {
1037:                            return "false";
1038:                        }
1039:                    } else if (key.equals("dbname")) {
1040:                        return def.getName();
1041:                    } else if (key.equals("dbsize")) {
1042:                        int size = def.getMaxLength();
1043:                        if (size != -1) {
1044:                            return "" + size;
1045:                        } else {
1046:                            return "fixed";
1047:                        }
1048:                    } else if (key.equals("dbstate")) {
1049:                        return Fields.getStateDescription(def.getState());
1050:                    } else if (key.equals("dbmmbasetype")) {
1051:                        return Fields.getTypeDescription(def.getType());
1052:                    } else if (key.equals("editorinput")) {
1053:                        int pos = def.getEditPosition();
1054:                        if (pos == -1) {
1055:                            return "not shown";
1056:                        } else {
1057:                            return "" + pos;
1058:                        }
1059:                    } else if (key.equals("editorsearch")) {
1060:                        int pos = def.getSearchPosition();
1061:                        if (pos == -1) {
1062:                            return "not shown";
1063:                        } else {
1064:                            return "" + pos;
1065:                        }
1066:                    } else if (key.equals("editorlist")) {
1067:                        int pos = def.getListPosition();
1068:                        if (pos == -1) {
1069:                            return "not shown";
1070:                        } else {
1071:                            return "" + pos;
1072:                        }
1073:                    } else if (key.equals("guitype")) {
1074:                        return def.getGUIType();
1075:                    }
1076:                }
1077:                return "";
1078:            }
1079:
1080:            /**
1081:             * @javadoc
1082:             */
1083:            private Vector<String> getISOGuiNames(String buildername,
1084:                    String fieldname) {
1085:                Vector<String> results = new Vector<String>();
1086:                MMObjectBuilder bul = getBuilder(buildername);
1087:                if (bul != null) {
1088:                    CoreField def = bul.getField(fieldname);
1089:                    Map<Locale, String> guinames = def.getLocalizedGUIName()
1090:                            .asMap();
1091:                    for (Entry<Locale, String> me : guinames.entrySet()) {
1092:                        results.add(me.getKey().getLanguage());
1093:                        results.add(me.getValue());
1094:                    }
1095:                }
1096:                return results;
1097:            }
1098:
1099:            /**
1100:             * @javadoc
1101:             */
1102:            private Vector<String> getISODescriptions(String buildername,
1103:                    String fieldname) {
1104:                Vector<String> results = new Vector<String>();
1105:                MMObjectBuilder bul = getBuilder(buildername);
1106:                if (bul != null) {
1107:                    CoreField def = bul.getField(fieldname);
1108:                    Map<Locale, String> guinames = def
1109:                            .getLocalizedDescription().asMap();
1110:                    for (Entry<Locale, String> me : guinames.entrySet()) {
1111:                        results.add(me.getKey().getLanguage());
1112:                        results.add(me.getValue());
1113:                    }
1114:                }
1115:                return results;
1116:            }
1117:
1118:            /**
1119:             * @javadoc
1120:             */
1121:            private String getGuiNameValue(String buildername,
1122:                    String fieldname, String lang) {
1123:                MMObjectBuilder bul = getBuilder(buildername);
1124:                if (bul != null) {
1125:                    CoreField def = bul.getField(fieldname);
1126:                    String value = def.getGUIName(new Locale(lang, ""));
1127:                    if (value != null) {
1128:                        return value;
1129:                    }
1130:                }
1131:                return "";
1132:            }
1133:
1134:            /**
1135:             * @javadoc
1136:             */
1137:            private String getDescription(String buildername, String fieldname,
1138:                    String lang) {
1139:                MMObjectBuilder bul = getBuilder(buildername);
1140:                if (bul != null) {
1141:                    CoreField def = bul.getField(fieldname);
1142:                    String value = def.getDescription(new Locale(lang, ""));
1143:                    if (value != null) {
1144:                        return value;
1145:                    }
1146:                }
1147:                return "";
1148:            }
1149:
1150:            /**
1151:             * @javadoc
1152:             */
1153:            public void doModulePosts(String command,
1154:                    Hashtable<String, Object> cmds,
1155:                    Hashtable<String, Object> vars) {
1156:                if (command.equals("SETPROPERTY")) {
1157:                    setModuleProperty(vars);
1158:                }
1159:            }
1160:
1161:            /**
1162:             * @javadoc
1163:             */
1164:            public void doBuilderPosts(String command,
1165:                    Hashtable<String, Object> cmds,
1166:                    Hashtable<String, Object> vars) {
1167:                if (command.equals("SETGUINAME")) {
1168:                    setBuilderGuiName(vars);
1169:                } else if (command.equals("SETDESCRIPTION")) {
1170:                    setBuilderDescription(vars);
1171:                } else if (command.equals("SETGUITYPE")) {
1172:                    setBuilderGuiType(vars);
1173:                } else if (command.equals("SETEDITORINPUT")) {
1174:                    setBuilderEditorInput(vars);
1175:                } else if (command.equals("SETEDITORLIST")) {
1176:                    setBuilderEditorList(vars);
1177:                } else if (command.equals("SETEDITORSEARCH")) {
1178:                    setBuilderEditorSearch(vars);
1179:                } else if (command.equals("SETDBSIZE")) {
1180:                    setBuilderDBSize(vars);
1181:                } else if (command.equals("SETDBKEY")) {
1182:                    setBuilderDBKey(vars);
1183:                } else if (command.equals("SETDBNOTNULL")) {
1184:                    setBuilderDBNotNull(vars);
1185:                } else if (command.equals("SETDBMMBASETYPE")) {
1186:                    setBuilderDBField(vars);
1187:                } else if (command.equals("SETSTATE")) {
1188:                    setBuilderDBState(vars);
1189:                } else if (command.equals("ADDFIELD")) {
1190:                    addBuilderField(vars);
1191:                } else if (command.equals("REMOVEFIELD")) {
1192:                    removeBuilderField(vars);
1193:                }
1194:            }
1195:
1196:            /**
1197:             * @javadoc
1198:             */
1199:            public void setBuilderGuiName(Hashtable<String, Object> vars) {
1200:                if (kioskmode) {
1201:                    log.warn("refused gui name set, am in kiosk mode");
1202:                    return;
1203:                }
1204:                String builder = (String) vars.get("BUILDER");
1205:                String fieldname = (String) vars.get("FIELDNAME");
1206:                String country = (String) vars.get("COUNTRY");
1207:                String value = (String) vars.get("VALUE");
1208:
1209:                MMObjectBuilder bul = getBuilder(builder);
1210:                CoreField def = bul.getField(fieldname);
1211:                if (def != null) {
1212:                    def.setGUIName(value, new Locale(country, ""));
1213:                }
1214:
1215:                CloudModel cloudmodel = ModelsManager.getModel("default");
1216:                if (cloudmodel != null) {
1217:                    CloudModelBuilder cloudmodelbuilder = cloudmodel
1218:                            .getModelBuilder(builder);
1219:                    if (cloudmodelbuilder != null)
1220:                        cloudmodelbuilder.setGuiName(fieldname, country, value);
1221:                }
1222:            }
1223:
1224:            /**
1225:             * @javadoc
1226:             * @since MMBase-1.7
1227:             */
1228:            public void setBuilderDescription(Hashtable<String, Object> vars) {
1229:                if (kioskmode) {
1230:                    log.warn("refused gui name set, am in kiosk mode");
1231:                    return;
1232:                }
1233:                String builder = (String) vars.get("BUILDER");
1234:                String fieldname = (String) vars.get("FIELDNAME");
1235:                String country = (String) vars.get("COUNTRY");
1236:                String value = (String) vars.get("VALUE");
1237:
1238:                MMObjectBuilder bul = getBuilder(builder);
1239:                CoreField def = bul.getField(fieldname);
1240:                if (def != null) {
1241:                    def.setDescription(value, new Locale(country, ""));
1242:                }
1243:                // need to be rerouted syncBuilderXML(bul, builder);
1244:            }
1245:
1246:            /**
1247:             * @javadoc
1248:             */
1249:            public void setBuilderGuiType(Hashtable<String, Object> vars) {
1250:                if (kioskmode) {
1251:                    log.warn("refused gui type set, am in kiosk mode");
1252:                    return;
1253:                }
1254:                String builder = (String) vars.get("BUILDER");
1255:                String fieldName = (String) vars.get("FIELDNAME");
1256:                String guiType = (String) vars.get("VALUE");
1257:
1258:                MMObjectBuilder bul = getBuilder(builder);
1259:                CoreField def = bul.getField(fieldName);
1260:                if (def != null) {
1261:                    DataType<? extends Object> dataType;
1262:                    int type = def.getType();
1263:                    if (type == Field.TYPE_LIST) {
1264:                        dataType = DataTypes.getDataTypeInstance(guiType, def
1265:                                .getListItemType());
1266:                    } else {
1267:                        dataType = DataTypes.getDataTypeInstance(guiType, type);
1268:                    }
1269:                    def.setDataType(dataType);
1270:                    def.finish();
1271:                }
1272:                // need to be rerouted syncBuilderXML(bul, builder);
1273:            }
1274:
1275:            /**
1276:             * @javadoc
1277:             */
1278:            public void setBuilderEditorInput(Hashtable<String, Object> vars) {
1279:                if (kioskmode) {
1280:                    log.warn("refused editor input set, am in kiosk mode");
1281:                    return;
1282:                }
1283:                String builder = (String) vars.get("BUILDER");
1284:                String fieldname = (String) vars.get("FIELDNAME");
1285:                String value = (String) vars.get("VALUE");
1286:
1287:                MMObjectBuilder bul = getBuilder(builder);
1288:                CoreField def = bul.getField(fieldname);
1289:                if (def != null) {
1290:                    try {
1291:                        int i = Integer.parseInt(value);
1292:                        def.setEditPosition(i);
1293:                    } catch (Exception e) {
1294:                    }
1295:                }
1296:                // need to be rerouted syncBuilderXML(bul, builder);
1297:            }
1298:
1299:            /**
1300:             * @javadoc
1301:             */
1302:            public void setBuilderEditorList(Hashtable<String, Object> vars) {
1303:                if (kioskmode) {
1304:                    log.warn("refused editor list set, am in kiosk mode");
1305:                    return;
1306:                }
1307:                String builder = (String) vars.get("BUILDER");
1308:                String fieldname = (String) vars.get("FIELDNAME");
1309:                String value = (String) vars.get("VALUE");
1310:
1311:                MMObjectBuilder bul = getBuilder(builder);
1312:                CoreField def = bul.getField(fieldname);
1313:                if (def != null) {
1314:                    try {
1315:                        int i = Integer.parseInt(value);
1316:                        def.setListPosition(i);
1317:                    } catch (Exception e) {
1318:                    }
1319:                }
1320:                // need to be rerouted syncBuilderXML(bul, builder);
1321:            }
1322:
1323:            /**
1324:             * @javadoc
1325:             */
1326:            public void setBuilderEditorSearch(Hashtable<String, Object> vars) {
1327:                if (kioskmode) {
1328:                    log.warn("refused editor pos set, am in kiosk mode");
1329:                    return;
1330:                }
1331:                String builder = (String) vars.get("BUILDER");
1332:                String fieldname = (String) vars.get("FIELDNAME");
1333:                String value = (String) vars.get("VALUE");
1334:
1335:                MMObjectBuilder bul = getBuilder(builder);
1336:                CoreField def = bul.getField(fieldname);
1337:                if (def != null) {
1338:                    try {
1339:                        int i = Integer.parseInt(value);
1340:                        def.setSearchPosition(i);
1341:                    } catch (Exception e) {
1342:                    }
1343:                }
1344:                // need to be rerouted syncBuilderXML(bul, builder);
1345:            }
1346:
1347:            /**
1348:             * @javadoc
1349:             */
1350:            public void setBuilderDBSize(Hashtable<String, Object> vars) {
1351:                if (kioskmode) {
1352:                    log.warn("Refused set DBSize field, am in kiosk mode");
1353:                    return;
1354:                }
1355:                String builder = (String) vars.get("BUILDER");
1356:                String fieldname = (String) vars.get("FIELDNAME");
1357:                String value = (String) vars.get("VALUE");
1358:
1359:                MMObjectBuilder bul = getBuilder(builder);
1360:                CoreField def = bul.getField(fieldname);
1361:                if (def != null) {
1362:                    int oldSize = def.getMaxLength();
1363:                    try {
1364:                        int newSize = Integer.parseInt(value);
1365:                        if (newSize != oldSize) {
1366:                            def.rewrite();
1367:                            try {
1368:                                def.setMaxLength(newSize);
1369:                                // make change in storage
1370:                                mmb.getStorageManager().change(def);
1371:                                CloudModel cloudmodel = ModelsManager
1372:                                        .getModel("default");
1373:                                if (cloudmodel != null) {
1374:                                    CloudModelBuilder cloudmodelbuilder = cloudmodel
1375:                                            .getModelBuilder(builder);
1376:                                    if (cloudmodelbuilder != null)
1377:                                        cloudmodelbuilder.setBuilderDBSize(
1378:                                                fieldname, value);
1379:                                }
1380:                            } catch (StorageException se) {
1381:                                def.setMaxLength(oldSize);
1382:                                throw se;
1383:                            } finally {
1384:                                def.finish();
1385:                            }
1386:                        }
1387:                    } catch (NumberFormatException nfe) {
1388:                        throw new RuntimeException(nfe);
1389:                    }
1390:                }
1391:            }
1392:
1393:            /**
1394:             * @javadoc
1395:             */
1396:            public void setBuilderDBField(Hashtable<String, Object> vars) {
1397:                if (kioskmode) {
1398:                    log.warn("Refused set setDBField field, am in kiosk mode");
1399:                    return;
1400:                }
1401:                String builder = (String) vars.get("BUILDER");
1402:                String fieldname = (String) vars.get("FIELDNAME");
1403:                String value = (String) vars.get("VALUE");
1404:
1405:                MMObjectBuilder bul = getBuilder(builder);
1406:                CoreField def = bul.getField(fieldname);
1407:                if (def != null) {
1408:                    int oldType = def.getType();
1409:                    int newType = Fields.getType(value);
1410:                    if (oldType != newType) {
1411:                        def.rewrite();
1412:                        def.setType(newType);
1413:                        try {
1414:                            // make change in storage
1415:                            mmb.getStorageManager().change(def);
1416:                            // need to be rerouted syncBuilderXML(bul, builder);
1417:                        } catch (StorageException se) {
1418:                            def.setType(oldType);
1419:                            throw se;
1420:                        } finally {
1421:                            def.finish();
1422:                        }
1423:                    }
1424:                }
1425:            }
1426:
1427:            /**
1428:             * @javadoc
1429:             */
1430:            public void setBuilderDBState(Hashtable<String, Object> vars) {
1431:                if (kioskmode) {
1432:                    log.warn("Refused set DBState field, am in kiosk mode");
1433:                    return;
1434:                }
1435:                log.info("SET DBDSTATE");
1436:                String builder = (String) vars.get("BUILDER");
1437:                String fieldname = (String) vars.get("FIELDNAME");
1438:                String value = (String) vars.get("VALUE");
1439:
1440:                MMObjectBuilder bul = getBuilder(builder);
1441:                CoreField def = bul.getField(fieldname);
1442:                if (def != null) {
1443:                    int oldState = def.getState();
1444:                    int newState = Fields.getState(value);
1445:                    if (oldState != newState) {
1446:                        boolean oldInStorage = def.inStorage();
1447:                        def.rewrite();
1448:                        def.setState(newState);
1449:                        try {
1450:                            // add field if it was not persistent before
1451:                            if (def.inStorage() && !oldInStorage) {
1452:                                // make change in storage
1453:                                mmb.getStorageManager().create(def);
1454:                                // only then add to builder
1455:                                bul.addField(def);
1456:                                // need to be rerouted syncBuilderXML(bul, builder);
1457:                            }
1458:                        } catch (StorageException se) {
1459:                            def.setState(oldState);
1460:                            throw se;
1461:                        } finally {
1462:                            def.finish();
1463:                        }
1464:                        CloudModel cloudmodel = ModelsManager
1465:                                .getModel("default");
1466:                        if (cloudmodel != null) {
1467:                            CloudModelBuilder cloudmodelbuilder = cloudmodel
1468:                                    .getModelBuilder(builder);
1469:                            if (cloudmodelbuilder != null)
1470:                                cloudmodelbuilder.setBuilderDBState(fieldname,
1471:                                        value);
1472:                        }
1473:                    }
1474:                }
1475:            }
1476:
1477:            /**
1478:             * @javadoc
1479:             */
1480:            public void setBuilderDBKey(Hashtable<String, Object> vars) {
1481:                if (kioskmode) {
1482:                    log.warn("Refused set dbkey field, am in kiosk mode");
1483:                    return;
1484:                }
1485:                log.info("SET DBKEY");
1486:                String builder = (String) vars.get("BUILDER");
1487:                String fieldname = (String) vars.get("FIELDNAME");
1488:                String value = (String) vars.get("VALUE");
1489:
1490:                MMObjectBuilder bul = getBuilder(builder);
1491:                CoreField def = bul.getField(fieldname);
1492:                if (def != null) {
1493:                    def.rewrite();
1494:                    if (value.equals("true")) {
1495:                        def.setUnique(true);
1496:                    } else {
1497:                        def.setUnique(false);
1498:                    }
1499:                    CloudModel cloudmodel = ModelsManager.getModel("default");
1500:                    if (cloudmodel != null) {
1501:                        CloudModelBuilder cloudmodelbuilder = cloudmodel
1502:                                .getModelBuilder(builder);
1503:                        if (cloudmodelbuilder != null)
1504:                            cloudmodelbuilder.setBuilderDBKey(fieldname, value);
1505:                    }
1506:                    def.finish();
1507:                }
1508:                // TODO: when changing key, should call CHANGE
1509:                // need to be rerouted syncBuilderXML(bul, builder);
1510:            }
1511:
1512:            /**
1513:             * @javadoc
1514:             */
1515:            public void setBuilderDBNotNull(Hashtable<String, Object> vars) {
1516:                if (kioskmode) {
1517:                    log.warn("Refused set NotNull field, am in kiosk mode");
1518:                    return;
1519:                }
1520:                String builder = (String) vars.get("BUILDER");
1521:                String fieldname = (String) vars.get("FIELDNAME");
1522:                String value = (String) vars.get("VALUE");
1523:
1524:                MMObjectBuilder bul = getBuilder(builder);
1525:                CoreField def = bul.getField(fieldname);
1526:                if (def != null) {
1527:                    boolean oldNotNull = def.isRequired();
1528:                    boolean newNotNull = value.equals("true");
1529:                    if (oldNotNull != newNotNull) {
1530:                        def.rewrite();
1531:                        def.getDataType().setRequired(newNotNull);
1532:                        try {
1533:                            // make change in storage
1534:                            mmb.getStorageManager().change(def);
1535:                            CloudModel cloudmodel = ModelsManager
1536:                                    .getModel("default");
1537:                            if (cloudmodel != null) {
1538:                                CloudModelBuilder cloudmodelbuilder = cloudmodel
1539:                                        .getModelBuilder(builder);
1540:                                if (cloudmodelbuilder != null)
1541:                                    cloudmodelbuilder.setBuilderDBNotNull(
1542:                                            fieldname, value);
1543:                            }
1544:                            // need to be rerouted syncBuilderXML(bul, builder);
1545:                        } catch (StorageException se) {
1546:                            def.getDataType().setRequired(oldNotNull);
1547:                            throw se;
1548:                        } finally {
1549:                            def.finish();
1550:                        }
1551:                    }
1552:                }
1553:            }
1554:
1555:            /**
1556:             * @javadoc
1557:             */
1558:            public void addBuilderField(Map<String, Object> vars) {
1559:                if (kioskmode) {
1560:                    log.warn("Refused add builder field, am in kiosk mode");
1561:                    return;
1562:                }
1563:                String builder = (String) vars.get("BUILDER");
1564:                MMObjectBuilder bul = getBuilder(builder);
1565:                if (bul != null) {
1566:                    // Determine position of new field.
1567:                    // This should be the number of the last field as denied in the builder xml,
1568:                    // as the DBPos field is incremented for each field in that file.
1569:                    int pos = bul.getFields(NodeManager.ORDER_CREATE).size() + 1;
1570:
1571:                    String fieldName = (String) vars.get("dbname");
1572:                    String guiType = (String) vars.get("guitype");
1573:                    int type = Fields.getType((String) vars.get("mmbasetype"));
1574:                    int itemListType = Fields.getType((String) vars
1575:                            .get("mmbasetype"));
1576:                    int state = Fields.getState((String) vars.get("dbstate"));
1577:
1578:                    log.service("Adding field " + fieldName);
1579:                    DataType<? extends Object> dataType;
1580:                    if (type == Field.TYPE_LIST) {
1581:                        dataType = DataTypes.getListDataTypeInstance(guiType,
1582:                                itemListType);
1583:                    } else {
1584:                        dataType = DataTypes.getDataTypeInstance(guiType, type);
1585:                    }
1586:                    log.debug("Found datatype " + dataType);
1587:
1588:                    CoreField def = Fields.createField(fieldName, type,
1589:                            itemListType, state, dataType);
1590:                    def.setListPosition(pos);
1591:                    def.setEditPosition(pos);
1592:
1593:                    def.setParent(bul);
1594:                    def.setStoragePosition(pos);
1595:
1596:                    String value = (String) vars.get("dbnotnull");
1597:                    def.getDataType().setRequired(value.equals("true"));
1598:
1599:                    value = (String) vars.get("dbkey");
1600:                    def.setUnique(value.equals("true"));
1601:
1602:                    value = (String) vars.get("dbsize");
1603:                    try {
1604:                        int i = Integer.parseInt(value);
1605:                        def.setMaxLength(i);
1606:                    } catch (Exception e) {
1607:                        log.debug("dbsize had invalid value, not setting size");
1608:                    }
1609:
1610:                    log.debug("Found field definition " + def);
1611:
1612:                    log.trace("Adding to storage");
1613:                    // make change in storage
1614:                    mmb.getStorageManager().create(def);
1615:                    log.trace("Adding to builder");
1616:                    // only then add to builder
1617:                    bul.addField(def);
1618:
1619:                    CloudModel cloudmodel = ModelsManager.getModel("default");
1620:                    if (cloudmodel != null) {
1621:                        log.debug("Calling cloud module builder");
1622:                        CloudModelBuilder cloudmodelbuilder = cloudmodel
1623:                                .getModelBuilder(builder);
1624:                        if (cloudmodelbuilder != null) {
1625:                            cloudmodelbuilder.addField(pos, fieldName,
1626:                                    (String) vars.get("mmbasetype"),
1627:                                    (String) vars.get("guitype"), (String) vars
1628:                                            .get("dbstate"), (String) vars
1629:                                            .get("dbnotnull"), (String) vars
1630:                                            .get("dbkey"), (String) vars
1631:                                            .get("dbsize"));
1632:                        }
1633:                    } else {
1634:                        log.warn("No cloud model 'default' found");
1635:                    }
1636:                    def.finish();
1637:                } else {
1638:                    log.service("Cannot add field to builder " + builder
1639:                            + " because it could not be found");
1640:                }
1641:            }
1642:
1643:            /**
1644:             * @javadoc
1645:             */
1646:            public void removeBuilderField(Hashtable<String, Object> vars) {
1647:                if (kioskmode) {
1648:                    log.warn("Refused remove builder field, am in kiosk mode");
1649:                    return;
1650:                }
1651:                String builder = (String) vars.get("BUILDER");
1652:                String fieldname = (String) vars.get("FIELDNAME");
1653:                String value = (String) vars.get("SURE");
1654:
1655:                MMObjectBuilder bul = getBuilder(builder);
1656:                if (bul != null && value != null && value.equals("Yes")) {
1657:
1658:                    CoreField def = bul.getField(fieldname);
1659:                    // make change in storage
1660:                    mmb.getStorageManager().delete(def);
1661:                    // only then delete in builder
1662:                    bul.removeField(fieldname);
1663:
1664:                    CloudModel cloudmodel = ModelsManager.getModel("default");
1665:                    if (cloudmodel != null) {
1666:                        CloudModelBuilder cloudmodelbuilder = cloudmodel
1667:                                .getModelBuilder(builder);
1668:                        if (cloudmodelbuilder != null)
1669:                            cloudmodelbuilder.removeField(fieldname);
1670:                    }
1671:                    def.finish();
1672:                }
1673:            }
1674:
1675:            /**
1676:             * @javadoc
1677:             */
1678:            public void syncBuilderXML(MMObjectBuilder bul, String builder) {
1679:                String savepath = MMBaseContext.getConfigPath() + "/builders/"
1680:                        + builder + ".xml";
1681:                log.service("Syncing builder xml (" + savepath
1682:                        + ") for builder " + builder);
1683:                try {
1684:                    BuilderWriter builderOut = new BuilderWriter(bul);
1685:                    builderOut.setIncludeComments(false);
1686:                    builderOut.setExpandBuilder(false);
1687:                    builderOut.writeToFile(savepath);
1688:                } catch (Exception e) {
1689:                    log.error(Logging.stackTrace(e));
1690:                }
1691:            }
1692:
1693:            /**
1694:             * @javadoc
1695:             */
1696:            public void syncModuleXML(Module mod, String modname) {
1697:                String savepath = MMBaseContext.getConfigPath()
1698:                        + File.separator + "modules" + File.separator + modname
1699:                        + ".xml";
1700:                try {
1701:                    ModuleWriter moduleOut = new ModuleWriter(mod);
1702:                    moduleOut.setIncludeComments(false);
1703:                    moduleOut.writeToFile(savepath);
1704:                } catch (Exception e) {
1705:                    log.error(Logging.stackTrace(e));
1706:                }
1707:            }
1708:
1709:            /**
1710:             * @javadoc
1711:             */
1712:            public Vector<String> getMultilevelCacheEntries() {
1713:                Vector<String> results = new Vector<String>();
1714:                for (Map.Entry<org.mmbase.storage.search.SearchQuery, List<MMObjectNode>> entry : MultilevelCache
1715:                        .getCache().entrySet()) {
1716:                    /*
1717:                    StringTagger tagger=en.getTagger();
1718:                    Vector type=tagger.Values("TYPE");
1719:                    Vector where=tagger.Values("WHERE");
1720:                    Vector dbsort=tagger.Values("DBSORT");
1721:                    Vector dbdir=tagger.Values("DBDIR");
1722:                    Vector fields=tagger.Values("FIELDS");
1723:                    results.add(""+en.getKey());
1724:                    results.add(""+type);
1725:                    results.add(""+fields);
1726:                    if (where!=null) {
1727:                        results.add(where.toString());
1728:                    } else {
1729:                        results.add("");
1730:                    }
1731:                    if (dbsort!=null) {
1732:                        results.add(dbsort.toString());
1733:                    } else {
1734:                        results.add("");
1735:                    }
1736:                    if (dbdir!=null) {
1737:                        results.add(dbdir.toString());
1738:                    } else {
1739:                        results.add("");
1740:                    }
1741:                    results.add(tagger.ValuesString("ALL"));
1742:                     */
1743:                    results.add(entry.getKey().toString());
1744:                    results.add(""
1745:                            + MultilevelCache.getCache().getCount(
1746:                                    entry.getKey()));
1747:                }
1748:                return results;
1749:            }
1750:
1751:            /**
1752:             * @javadoc
1753:             */
1754:            public Vector<String> getNodeCacheEntries() {
1755:                Vector<String> results = new Vector<String>();
1756:                Iterator<MMObjectNode> iter = NodeCache.getCache().values()
1757:                        .iterator();
1758:                while (iter.hasNext()) {
1759:                    MMObjectNode node = iter.next();
1760:                    results.add(""
1761:                            + NodeCache.getCache().getCount(
1762:                                    node.getIntegerValue("number")));
1763:                    results.add("" + node.getIntValue("number"));
1764:                    results.add(node.getStringValue("owner"));
1765:                    results.add(mmb.getTypeDef().getValue(
1766:                            node.getIntValue("otype")));
1767:                }
1768:                return results;
1769:            }
1770:
1771:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.