001: package com.sun.portal.search.admin.mbeans.tasks;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.util.logging.*;
006: import java.net.*;
007: import java.lang.*;
008: import java.text.*;
009:
010: import com.sun.portal.admin.server.mbeans.PSResource;
011: import com.sun.portal.admin.common.context.PortalDomainContext;
012: import com.sun.portal.admin.common.context.PSConfigContext;
013: import com.sun.portal.admin.common.PSMBeanException;
014:
015: import com.sun.portal.search.admin.CSDebug;
016: import com.sun.portal.search.admin.CSConfig;
017: import com.sun.portal.search.soif.*;
018: import com.sun.portal.search.rdm.*;
019: import com.sun.portal.search.rdmserver.*;
020:
021: import com.sun.portal.search.admin.mbeans.*;
022:
023: public class Schema {
024:
025: RDMSchema schema = null;
026: private String searchServerRoot = null;
027: private String defaultSchemaFileName = null;
028: private String schemaFileName = null;
029: private static Logger logger = null;
030:
031: public Schema(String psDir, String searchServerRoot, Logger logger)
032: throws PSMBeanException {
033: this .searchServerRoot = searchServerRoot;
034: this .logger = logger;
035:
036: defaultSchemaFileName = psDir + File.separator + "samples"
037: + File.separator + "schema" + File.separator
038: + "document.soif";
039:
040: schemaFileName = searchServerRoot + File.separator + "config"
041: + File.separator + "schema.rdm";
042:
043: try {
044: schema = loadSchema();
045: } catch (Exception e) {
046: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
047: throw new PSMBeanException("PSALI_CSPACCSH0001", e
048: .toString(), e);
049: }
050: }
051:
052: public void create(String name, String description, String aliases,
053: Boolean editable, Boolean indexable,
054: String scoreMultiplier, String dataType)
055: throws PSMBeanException {
056:
057: int column = schema.getColumnNumber(name);
058: if (column != -1) {
059: String message = "Schema already exists - " + name;
060: Object tokens[] = { message };
061: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
062: throw new PSMBeanException("PSALI_CSPACCSH0001");
063: }
064:
065: if (!validateName(name)) {
066: String message = "Invalid schema name - " + name;
067: Object tokens[] = { message };
068: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
069: throw new PSMBeanException("PSALI_CSPACCSH0001");
070: }
071:
072: if (!validateScoreMultiplier(scoreMultiplier)) {
073: String message = "Invalid schema score multiplier - "
074: + scoreMultiplier;
075: Object tokens[] = { message };
076: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
077: throw new PSMBeanException("PSALI_CSPACCSH0001");
078: }
079:
080: int entries = schema.getNumEntriesInt();
081:
082: column = schema.getMaxIndex() + 1;
083: schema.setSOIFAttribute(column, name);
084: schema.setColumnName(column, name);
085: schema.setSysColumnName(column, name);
086: schema.setTableName(column, "Documents");
087: schema.setSysTableName(column, "doctbl");
088: schema.setDescription(column, description);
089: schema.setAliases(column, aliases);
090: schema.setEditAttribute(column, (editable.booleanValue()) ? "1"
091: : "0");
092: schema.setIndexAttribute(column,
093: (indexable.booleanValue()) ? "1" : "0");
094: schema.setDataType(column, dataType);
095: if (!scoreMultiplier.equals("")) {
096: schema.setValue(name, RDM.A_RDM_SCORE_MULTIPLIER,
097: scoreMultiplier);
098: }
099:
100: schema.setNumEntries(new Integer(entries + 1).toString());
101:
102: try {
103: saveSchema(schema);
104: } catch (Exception e) {
105: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
106: throw new PSMBeanException("PSALI_CSPACCSH0001", e
107: .toString(), e);
108: }
109: }
110:
111: public void delete(String name) throws PSMBeanException {
112: int column = schema.getColumnNumber(name);
113: if (column == -1) {
114: String message = "Schema does not exists - " + name;
115: Object tokens[] = { message };
116: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
117: throw new PSMBeanException("PSALI_CSPACCSH0001");
118: }
119:
120: schema.deleteColumn(name);
121:
122: try {
123: saveSchema(schema);
124: } catch (Exception e) {
125: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
126: throw new PSMBeanException("PSALI_CSPACCSH0001", e
127: .toString(), e);
128: }
129: }
130:
131: public void restore() throws PSMBeanException {
132: try {
133: copyFile(defaultSchemaFileName, schemaFileName);
134: schema = loadSchema();
135: } catch (Exception e) {
136: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
137: throw new PSMBeanException("PSALI_CSPACCSH0001", e
138: .toString(), e);
139: }
140: }
141:
142: public ArrayList getAll() {
143: ArrayList data = new ArrayList();
144: for (int index = 0; index <= schema.getMaxIndex(); index++) {
145: String attribute = schema.getSOIFAttribute(index);
146: if (attribute != null) {
147: data.add(attribute);
148: }
149: }
150: return data;
151: }
152:
153: public Properties getAttributes(String name)
154: throws PSMBeanException {
155: int column = schema.getColumnNumber(name);
156: if (column == -1) {
157: String message = "Schema does not exists - " + name;
158: Object tokens[] = { message };
159: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
160: throw new PSMBeanException("PSALI_CSPACCSH0001");
161: }
162:
163: Properties p = new Properties();
164:
165: p.setProperty("name", name);
166:
167: String description = schema.getValue(name, RDM.A_RDM_DESC_ATTR);
168: p.setProperty("description", (description == null) ? ""
169: : description);
170:
171: String aliases = schema.getValue(name, RDM.A_RDM_ALIASES);
172: p.setProperty("aliases", (aliases == null) ? "" : aliases);
173:
174: String editable = schema.getValue(name, RDM.A_RDM_EDIT_ATTR);
175: if (editable != null) {
176: if (editable.equals("1")) {
177: p.setProperty("editable", "true");
178: } else {
179: p.setProperty("editable", "false");
180: }
181: } else {
182: p.setProperty("editable", "false");
183: }
184:
185: String indexable = schema.getValue(name, RDM.A_RDM_INDEX_ATTR);
186: if (indexable != null) {
187: if (indexable.equals("1")) {
188: p.setProperty("indexable", "true");
189: } else {
190: p.setProperty("indexable", "false");
191: }
192: } else {
193: p.setProperty("indexable", "false");
194: }
195:
196: String scoreMultiplier = schema.getValue(name,
197: RDM.A_RDM_SCORE_MULTIPLIER);
198: if (scoreMultiplier != null) {
199: p.setProperty("score-multiplier", scoreMultiplier);
200: } else {
201: p.setProperty("score-multiplier", "");
202: }
203:
204: String dataType = schema.getValue(name, RDM.A_RDM_DATA_TYPE);
205: p.setProperty("datatype", (dataType == null) ? "string"
206: : dataType);
207:
208: String internal = schema.getValue(name, RDM.A_RDM_INTERNAL);
209: if (internal != null) {
210: if (internal.equals("1")) {
211: p.setProperty("noninternal", "false");
212: } else {
213: p.setProperty("noninternal", "true");
214: }
215: } else {
216: p.setProperty("noninternal", "true");
217: }
218:
219: return p;
220: }
221:
222: public void setAttributes(String schemaName, String name,
223: String description, String aliases, Boolean editable,
224: Boolean indexable, String scoreMultiplier, String dataType)
225: throws PSMBeanException {
226:
227: int column = schema.getColumnNumber(schemaName);
228: if (column == -1) {
229: String message = "Schema does not exists - " + schemaName;
230: Object tokens[] = { message };
231: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
232: throw new PSMBeanException("PSALI_CSPACCSH0001");
233: }
234:
235: if (!validateName(name)) {
236: String message = "Invalid schema name - " + name;
237: Object tokens[] = { message };
238: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
239: throw new PSMBeanException("PSALI_CSPACCSH0001");
240: }
241:
242: if (!validateScoreMultiplier(scoreMultiplier)) {
243: String message = "Invalid schema score multiplier - "
244: + scoreMultiplier;
245: Object tokens[] = { message };
246: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
247: throw new PSMBeanException("PSALI_CSPACCSH0001");
248: }
249:
250: schema.setSOIFAttribute(column, name);
251: schema.setColumnName(column, name);
252: schema.setSysColumnName(column, name);
253: schema.setDescription(column, description);
254: schema.setAliases(column, aliases);
255: schema.setEditAttribute(column, (editable.booleanValue()) ? "1"
256: : "0");
257: schema.setIndexAttribute(column,
258: (indexable.booleanValue()) ? "1" : "0");
259: schema.setDataType(column, dataType);
260: if (!scoreMultiplier.equals("")) {
261: schema.setValue(name, RDM.A_RDM_SCORE_MULTIPLIER,
262: scoreMultiplier);
263: }
264:
265: try {
266: saveSchema(schema);
267: } catch (Exception e) {
268: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
269: throw new PSMBeanException("PSALI_CSPACCSH0001", e
270: .toString(), e);
271: }
272: }
273:
274: private RDMSchema loadSchema() throws Exception {
275: SOIFInputStream is = new SOIFInputStream(schemaFileName);
276: return new RDMSchema(is.readSOIF());
277: }
278:
279: private void saveSchema(RDMSchema schema) throws Exception {
280: SOIFOutputStream os = new SOIFOutputStream(schemaFileName);
281: os.write(schema.getSOIF());
282: }
283:
284: private boolean validateName(String name) {
285: if (name.trim().equals("")) {
286: return false;
287: }
288: for (int index = 0; index < name.length(); index++) {
289: char c = name.charAt(index);
290: if (!Character.isLetterOrDigit(c) && c != '.' && c != '_'
291: && c != '-') {
292: return false;
293: }
294: }
295: return true;
296: }
297:
298: private boolean validateScoreMultiplier(String scoreMultiplier) {
299: if (!scoreMultiplier.equals("")) {
300: try {
301: Float f = new Float(scoreMultiplier);
302: if (f.doubleValue() < 0.0) {
303: return false;
304: }
305: } catch (NumberFormatException nfe) {
306: return false;
307: }
308: }
309: return true;
310: }
311:
312: private void copyFile(String fromFileName, String toFileName)
313: throws Exception {
314: File fromFile = new File(fromFileName);
315: File toFile = new File(toFileName);
316:
317: File parent = toFile.getParentFile();
318: if (parent != null) {
319: parent.mkdirs();
320: }
321:
322: FileInputStream fis = new FileInputStream(fromFile);
323: byte[] b = getByteData(fis);
324: fis.close();
325:
326: FileOutputStream fout = new FileOutputStream(toFile);
327: fout.write(b);
328: fout.close();
329: }
330:
331: private byte[] getByteData(InputStream is) throws Exception {
332: ByteArrayOutputStream os = new ByteArrayOutputStream();
333: byte[] byteBuf = new byte[1024];
334: int num = 0;
335: while ((num = is.read(byteBuf)) != -1) {
336: os.write(byteBuf, 0, num);
337: }
338: return os.toByteArray();
339: }
340:
341: }
|