|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
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 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class AddCollection extends Command { |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
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 |
| |
|
65 |
0
| config = readConfig(table);
|
|
66 |
| } else { |
|
67 |
| |
|
68 |
0
| config = buildConfig(table);
|
|
69 |
| } |
|
70 |
| |
|
71 |
| |
|
72 |
0
| Collection col = null;
|
|
73 |
0
| Collection tempcol = null;
|
|
74 |
0
| try {
|
|
75 |
| |
|
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 |
| |
|
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 |
| |
|
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 |
| |
|
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 |
| |
|
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 |
| |
|
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 |
| |
|
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 |
| |
|
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 |
| } |