Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 218   Methods: 4
NCLOC: 150   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AddCollection.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: AddCollection.java 765794 2009-04-16 22:32:28Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.tools.command;
 21   
 22    import org.apache.xindice.client.xmldb.services.CollectionManager;
 23    import org.apache.xindice.tools.XMLTools;
 24    import org.apache.xindice.xml.dom.DocumentImpl;
 25    import org.apache.xindice.xml.dom.DOMParser;
 26    import org.apache.xindice.xml.TextWriter;
 27    import org.apache.xindice.util.XindiceException;
 28   
 29    import org.w3c.dom.Document;
 30    import org.w3c.dom.Element;
 31    import org.w3c.dom.Attr;
 32    import org.xmldb.api.DatabaseManager;
 33    import org.xmldb.api.base.Collection;
 34   
 35    import java.io.InputStream;
 36    import java.io.File;
 37    import java.io.FileInputStream;
 38   
 39    /**
 40    * AddCollection.java is designed to let the user create
 41    * new Collections and Nested Collections within the Database.
 42    *
 43    * @version $Revision: 765794 $, $Date: 2009-04-16 22:32:28 +0000 (Thu, 16 Apr 2009) $
 44    */
 45    public class AddCollection extends Command {
 46   
 47    /**
 48    * Creates collection
 49    */
 50  0 public boolean execute(XMLTools.Config table) throws Exception {
 51   
 52  0 if (table.getString(XMLTools.COLLECTION) == null) {
 53  0 System.out.println("ERROR : Collection and switch required");
 54  0 return false;
 55    }
 56   
 57  0 if ("".equals(table.getString(XMLTools.FILE_PATH)) && table.getString(XMLTools.NAME_OF) == null) {
 58  0 System.out.println("ERROR : New Collection name required or File path required");
 59  0 return false;
 60    }
 61   
 62  0 Document config;
 63  0 if (!"".equals(table.getString(XMLTools.FILE_PATH))) {
 64    // configuration was passed in a file
 65  0 config = readConfig(table);
 66    } else {
 67    // build configuration from parameters
 68  0 config = buildConfig(table);
 69    }
 70   
 71   
 72  0 Collection col = null;
 73  0 Collection tempcol = null;
 74  0 try {
 75    // Get a Collection reference to pass on to individual commands
 76  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 77    table.getBoolean(XMLTools.LOCAL));
 78   
 79  0 col = DatabaseManager.getCollection(colstring);
 80  0 if (col == null) {
 81  0 System.out.println("ERROR : Collection not found!");
 82  0 return false;
 83    }
 84   
 85    // Create an instance of the collection manager and create the collection
 86  0 CollectionManager colman = (CollectionManager) col.getService("CollectionManager", XMLDBAPIVERSION);
 87   
 88  0 String colPath;
 89  0 if (!"".equals(table.getString(XMLTools.FILE_PATH))) {
 90  0 colPath = config.getDocumentElement().getAttributeNode("name").getNodeValue();
 91    } else {
 92  0 colPath = table.getString(XMLTools.NAME_OF);
 93    }
 94   
 95  0 tempcol = colman.createCollection(colPath, config);
 96   
 97  0 System.out.println("Created : " + table.getString(XMLTools.COLLECTION) + "/" + colPath);
 98    } finally {
 99    // Release the collection objects
 100  0 if (col != null) {
 101  0 col.close();
 102    }
 103  0 if (tempcol != null) {
 104  0 tempcol.close();
 105    }
 106    }
 107   
 108  0 return true;
 109    }
 110   
 111  0 private Document readConfig(XMLTools.Config table) throws Exception {
 112  0 InputStream fis = null;
 113  0 try {
 114  0 File file = new File(table.getString(XMLTools.FILE_PATH));
 115  0 fis = new FileInputStream(file);
 116   
 117    // check if top element has necessary attribute 'name'
 118  0 Document doc = DOMParser.toDocument(fis);
 119  0 Attr attr = doc.getDocumentElement().getAttributeNode("name");
 120  0 if (attr == null || "".equals(attr.getNodeValue())) {
 121  0 throw new Exception("Collection configuration top element must have attribute \"name\"");
 122    }
 123   
 124  0 return doc;
 125    } catch (XindiceException e) {
 126  0 throw new Exception("Indexer configuration could not be parsed", e);
 127    } finally {
 128  0 if (fis != null) {
 129  0 fis.close();
 130    }
 131    }
 132    }
 133   
 134  0 private Document buildConfig(XMLTools.Config table) throws Exception {
 135  0 String colPath = table.getString(XMLTools.NAME_OF);
 136   
 137  0 String colName;
 138    // get the part after last /, any collection before / must already exist
 139  0 int idx = colPath.lastIndexOf("/");
 140  0 if (idx != -1) {
 141  0 colName = colPath.substring(idx + 1);
 142    } else {
 143  0 colName = colPath;
 144    }
 145   
 146  0 if (colName.equals("")) {
 147  0 throw new Exception("Cannot create a NULL collection");
 148    }
 149   
 150  0 Document doc = new DocumentImpl();
 151   
 152  0 Element colEle = doc.createElement("collection");
 153  0 colEle.setAttribute("name", colName);
 154    // FIXME Make this configurable
 155  0 colEle.setAttribute("compressed", "true");
 156  0 colEle.setAttribute("inline-metadata", "true");
 157   
 158  0 doc.appendChild(colEle);
 159   
 160  0 Element filEle = doc.createElement("filer");
 161  0 String filerClass = "org.apache.xindice.core.filer.BTreeFiler";
 162    // see if user specified filer type
 163  0 if (table.getString(XMLTools.FILER) != null) {
 164  0 String filer = table.getString(XMLTools.FILER);
 165  0 if ("HashFiler".equals(filer)) {
 166  0 filerClass = "org.apache.xindice.core.filer.HashFiler";
 167  0 } else if (!"BTreeFiler".equals(filer)) {
 168  0 throw new Exception("Unknown filer: " + filer);
 169    }
 170    }
 171   
 172  0 filEle.setAttribute("class", filerClass);
 173  0 if (table.getString(XMLTools.PAGE_SIZE) != null) {
 174  0 filEle.setAttribute(XMLTools.PAGE_SIZE, table.getString(XMLTools.PAGE_SIZE));
 175    }
 176  0 if (table.getString(XMLTools.MAX_KEY_SIZE) != null) {
 177  0 filEle.setAttribute(XMLTools.MAX_KEY_SIZE, table.getString(XMLTools.MAX_KEY_SIZE));
 178    }
 179  0 if (table.getString(XMLTools.PAGE_COUNT) != null) {
 180  0 filEle.setAttribute(XMLTools.PAGE_COUNT, table.getString(XMLTools.PAGE_COUNT));
 181    }
 182   
 183  0 colEle.appendChild(filEle);
 184   
 185    // If in verbose mode, show....
 186  0 if (table.getBoolean(XMLTools.VERBOSE)) {
 187  0 String colstr = TextWriter.toString(doc);
 188  0 System.out.println("Collection node element = ");
 189  0 System.out.println("\t" + colstr + "\n");
 190    }
 191   
 192  0 return doc;
 193    }
 194  0 public void usage() {
 195  0 System.out.println("Format: xindice ac -c <context> [-l [-d <path>]] [-v] [parameters...]");
 196  0 System.out.println();
 197  0 System.out.println("Creates new collections and nested collections");
 198  0 System.out.println();
 199  0 System.out.println("Command-specific switches:");
 200  0 System.out.println(" -n|--nameOf <name>");
 201  0 System.out.println(" Name for the collection to be created, must be present");
 202  0 System.out.println(" unless collection configuration is specified");
 203  0 System.out.println(" -f|--filepath <file>");
 204  0 System.out.println(" Name of the file that holds collection configuration. If");
 205  0 System.out.println(" specified, the rest of command-specific parameters is");
 206  0 System.out.println(" ignored");
 207  0 System.out.println(" --pagesize <number>");
 208  0 System.out.println(" Page size for file pages (default: 4096)");
 209  0 System.out.println(" --maxkeysize <number>");
 210  0 System.out.println(" The maximum size for file keys (default: 0=none)");
 211  0 System.out.println(" --pagecount <number>");
 212  0 System.out.println(" Number of pages in the primary storage (default: 1024)");
 213  0 System.out.println(" --filer <name>");
 214  0 System.out.println(" Collection filer, can be HashFiler or BTreeFiler (default:");
 215  0 System.out.println(" BTreeFiler)");
 216  0 System.out.println();
 217    }
 218    }