KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > tool > JalistoViewer


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.tool;
25
26 import org.objectweb.jalisto.se.JalistoFactory;
27 import org.objectweb.jalisto.se.api.*;
28 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess;
29 import org.objectweb.jalisto.se.api.internal.LogicalSystemPageAccess;
30 import org.objectweb.jalisto.se.api.internal.SessionInternal;
31 import org.objectweb.jalisto.se.exception.JalistoException;
32 import org.objectweb.jalisto.se.impl.ExtentImpl;
33 import org.objectweb.jalisto.se.impl.LogicalOid;
34 import org.objectweb.jalisto.se.impl.server.SessionImpl;
35 import org.objectweb.jalisto.se.impl.meta.InternalMetaRepositoryImpl;
36
37 import java.io.PrintWriter JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41
42 /**
43  * Utility class to look into a Jalisto base, specified by his properties file.
44  * Call main method with no argument to print usage.
45  */

46 public class JalistoViewer {
47
48     public static void main(String JavaDoc[] args) {
49         try {
50             parseArgs(args);
51             JalistoViewer viewer = new JalistoViewer(JalistoFactory.getInternalFactory().getSession(propFilePath),
52                                              args,
53                                              new PrintWriter JavaDoc(System.out));
54             viewer.readBase();
55         } catch (Exception JavaDoc e) {
56             e.printStackTrace();
57         }
58     }
59
60     private static void printUsage(PrintWriter JavaDoc writer, String JavaDoc message) {
61         if ((message != null) && (!message.equals(""))) {
62             writer.println(message);
63         }
64         writer.println(
65                 "java org.objectweb.jalisto.JalistoViewer <option> : print persistent instances from specified datastore");
66         writer.println("\t" + FILE_OPTION + " <path> : specify the properties file of the base to inspect (mandatory)");
67         writer.println("\t" + NO_INSTANCE_OPTION + " : doesn't show instance");
68         writer.println("\t" + INSTANCE_OPTION + " <coma-separated floid list> : show only the floid-related instances");
69         writer.println("\t" + META_OPTION + " : show the class meta-model");
70         writer.println("\t" + STATE_OPTION + " : show the initial state of the base");
71     }
72
73
74     public JalistoViewer(Session session, String JavaDoc[] args, PrintWriter JavaDoc out) {
75         try {
76             parseArgs(args);
77         } catch (Exception JavaDoc e) {
78             printUsage(out, e.getMessage());
79             out.flush();
80             System.exit(0);
81         }
82
83         this.writer = out;
84         if (session == null) {
85             this.session = JalistoFactory.getSession(propFilePath);
86         } else {
87             this.session = session;
88         }
89         session.openSession();
90         fileAccess = ((SessionInternal)session).getFileAccess();
91         repository = (InternalMetaRepositoryImpl) session.getMetaRepository();
92     }
93
94     public void readBase() {
95         if (withState) {
96             printState(0);
97         }
98         session.currentTransaction().begin();
99         Iterator JavaDoc classNameIterator = getAllClassNames().iterator();
100         while (classNameIterator.hasNext()) {
101             String JavaDoc className = (String JavaDoc) classNameIterator.next();
102             Collection JavaDoc oids = getExtent(className);
103             if (withMeta) {
104                 printClassMeta(1, className, oids);
105             }
106             if ((!withSelectedInstance) && (!noInstance)) {
107                 printClassInstances(1, className, oids);
108             }
109         }
110         if (withSelectedInstance && (!noInstance)) {
111             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(instanceList, ",");
112             while (tokenizer.hasMoreTokens()) {
113                 long l = Long.parseLong(tokenizer.nextToken());
114                 LogicalOid floid = new LogicalOid(l);
115                 Short JavaDoc clid = new Short JavaDoc(floid.getClid());
116                 ClassDescription meta = repository.getPersistentClassMetaDescription(clid);
117                 printInstance(1, meta, floid);
118             }
119         }
120         session.currentTransaction().commit();
121         writer.flush();
122     }
123
124     private Collection JavaDoc getAllClassNames() {
125         return repository.getAllClassNames();
126     }
127
128     private Collection JavaDoc getExtent(String JavaDoc fullClassName) {
129         Extent extent = new ExtentImpl(repository.getClidFromClassName(fullClassName),
130                                              (SessionInternal) session);
131         return extent.readFully().collection();
132     }
133
134     private static String JavaDoc getTab(int nbrTab) {
135         String JavaDoc tab = "";
136         for (int i = 0; i < nbrTab; i++) {
137             tab = tab + "\t";
138         }
139         return tab;
140     }
141
142     private static void parseArgs(String JavaDoc[] args) {
143         if (args.length == 0) {
144             throw new JalistoException("FileViewer : no arguments");
145         }
146
147         boolean hasFileOption = false;
148
149         for (int i = 0; i < args.length; i++) {
150             String JavaDoc o = args[i];
151
152             if (o.equalsIgnoreCase(FILE_OPTION)) {
153                 propFilePath = args[i + 1];
154                 hasFileOption = true;
155                 i++;
156             } else if (o.equalsIgnoreCase(INSTANCE_OPTION)) {
157                 instanceList = args[i + 1];
158                 i++;
159                 withSelectedInstance = true;
160             } else if (o.equalsIgnoreCase(NO_INSTANCE_OPTION)) {
161                 noInstance = true;
162             } else if (o.equalsIgnoreCase(META_OPTION)) {
163                 withMeta = true;
164             } else if (o.equalsIgnoreCase(STATE_OPTION)) {
165                 withState = true;
166             } else if (o.equalsIgnoreCase(HELP_OPTION)) {
167                 throw new JalistoException("FileViewer : print help option");
168             } else {
169                 throw new JalistoException("FileViewer : invalid argument format : " + o);
170             }
171         }
172
173         if (!hasFileOption) {
174             throw new JalistoException("FileViewer : no -file option");
175         }
176     }
177
178     private void printClassInstances(int nbrTab, String JavaDoc className, Collection JavaDoc oids) {
179         Iterator JavaDoc oidsIt = oids.iterator();
180         Object JavaDoc clid = repository.getClidFromClassName(className);
181         ClassDescription meta = repository.getPersistentClassMetaDescription(clid);
182         while (oidsIt.hasNext()) {
183             LogicalOid floid = (LogicalOid) oidsIt.next();
184             printInstance(nbrTab, meta, floid);
185         }
186         writer.println();
187     }
188
189     private void printClassMeta(int nbrTab, String JavaDoc className, Collection JavaDoc oids) {
190         String JavaDoc tab = getTab(nbrTab);
191         String JavaDoc tabPlus1 = getTab(nbrTab + 1);
192         Object JavaDoc clid = repository.getClidFromClassName(className);
193         ClassDescription meta = repository.getPersistentClassMetaDescription(clid);
194         String JavaDoc nbrInstanceMessage;
195         int size = oids.size();
196         if (size == 0) {
197             nbrInstanceMessage = "no instances";
198         } else if (size == 1) {
199             nbrInstanceMessage = "1 instance";
200         } else {
201             nbrInstanceMessage = String.valueOf(size) + " instances";
202         }
203         writer.println(className + " : " + nbrInstanceMessage);
204         writer.println(tab + "Meta-description of class " + className);
205         writer.println(tabPlus1 + "Instance number : " + nbrInstanceMessage);
206         String JavaDoc[] fieldNames = meta.getFieldNames();
207         for (int i = 0; i < fieldNames.length; i++) {
208             writer.println(tabPlus1 + "field " + i + " name : " + fieldNames[i] +
209                            ", type : " + meta.getFieldDescription(i).getType());
210         }
211         writer.println();
212     }
213
214     private void printInstance(int nbrTab, ClassDescription meta, LogicalOid floid) {
215         String JavaDoc tab = getTab(nbrTab);
216         String JavaDoc tabPlus1 = getTab(nbrTab + 1);
217         if (meta == null) {
218             writer.println(tab + "Instance number : " + floid);
219             writer.println(tabPlus1 + "object null in base");
220             writer.println();
221             return;
222         }
223         writer.println(tab + "Instance of " + meta.getClassName() + " number : " + floid);
224         String JavaDoc[] fields = meta.getFieldNames();
225         Object JavaDoc[] o = session.readObjectByOid(floid);
226         if (o != null) {
227             for (int i = 0; i < o.length; i++) {
228                 String JavaDoc objectInString;
229                 if (o[i] instanceof String JavaDoc) {
230                     objectInString = "\"" + o[i] + "\"";
231                 } else {
232                     objectInString = String.valueOf(o[i]);
233                 }
234                 writer.println(tabPlus1 + fields[i] + " : " + objectInString);
235             }
236         } else {
237             writer.println(tabPlus1 + "object null in base");
238         }
239         writer.println();
240     }
241
242     private void printState(int nbrTab) {
243         String JavaDoc tab = getTab(nbrTab);
244         String JavaDoc tabPlus1 = getTab(nbrTab + 1);
245         InternalPhysicalFileAccess physical = fileAccess.getPhysicalAccess();
246         JalistoProperties state = (JalistoProperties) physical.readFileObjectAt(SessionImpl.BSTATE_IFA).getData();
247         Iterator JavaDoc propIt = state.getKeys().iterator();
248         writer.println(tab + "State of base :");
249         while (propIt.hasNext()) {
250             String JavaDoc key = (String JavaDoc) propIt.next();
251             writer.println(tabPlus1 + key + " = " + state.getProperty(key));
252         }
253         writer.println();
254     }
255
256     private Session session;
257     private InternalMetaRepositoryImpl repository;
258     private LogicalSystemPageAccess fileAccess;
259     private PrintWriter JavaDoc writer;
260
261
262     private static String JavaDoc propFilePath;
263     private static String JavaDoc instanceList;
264     private static boolean withMeta = false;
265     private static boolean withState = false;
266     private static boolean withSelectedInstance = false;
267     private static boolean noInstance = false;
268     private static final String JavaDoc FILE_OPTION = "-file";
269     private static final String JavaDoc INSTANCE_OPTION = "-i";
270     private static final String JavaDoc NO_INSTANCE_OPTION = "-ni";
271     private static final String JavaDoc META_OPTION = "-m";
272     private static final String JavaDoc STATE_OPTION = "-s";
273     private static final String JavaDoc HELP_OPTION = "-h";
274
275 }
276
Popular Tags