1 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 ; 38 import java.util.Collection ; 39 import java.util.Iterator ; 40 import java.util.StringTokenizer ; 41 42 46 public class JalistoViewer { 47 48 public static void main(String [] args) { 49 try { 50 parseArgs(args); 51 JalistoViewer viewer = new JalistoViewer(JalistoFactory.getInternalFactory().getSession(propFilePath), 52 args, 53 new PrintWriter (System.out)); 54 viewer.readBase(); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 } 58 } 59 60 private static void printUsage(PrintWriter writer, String 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 [] args, PrintWriter out) { 75 try { 76 parseArgs(args); 77 } catch (Exception 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 classNameIterator = getAllClassNames().iterator(); 100 while (classNameIterator.hasNext()) { 101 String className = (String ) classNameIterator.next(); 102 Collection 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 tokenizer = new StringTokenizer (instanceList, ","); 112 while (tokenizer.hasMoreTokens()) { 113 long l = Long.parseLong(tokenizer.nextToken()); 114 LogicalOid floid = new LogicalOid(l); 115 Short clid = new Short (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 getAllClassNames() { 125 return repository.getAllClassNames(); 126 } 127 128 private Collection getExtent(String fullClassName) { 129 Extent extent = new ExtentImpl(repository.getClidFromClassName(fullClassName), 130 (SessionInternal) session); 131 return extent.readFully().collection(); 132 } 133 134 private static String getTab(int nbrTab) { 135 String 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 [] 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 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 className, Collection oids) { 179 Iterator oidsIt = oids.iterator(); 180 Object 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 className, Collection oids) { 190 String tab = getTab(nbrTab); 191 String tabPlus1 = getTab(nbrTab + 1); 192 Object clid = repository.getClidFromClassName(className); 193 ClassDescription meta = repository.getPersistentClassMetaDescription(clid); 194 String 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 [] 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 tab = getTab(nbrTab); 216 String 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 [] fields = meta.getFieldNames(); 225 Object [] o = session.readObjectByOid(floid); 226 if (o != null) { 227 for (int i = 0; i < o.length; i++) { 228 String objectInString; 229 if (o[i] instanceof String ) { 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 tab = getTab(nbrTab); 244 String tabPlus1 = getTab(nbrTab + 1); 245 InternalPhysicalFileAccess physical = fileAccess.getPhysicalAccess(); 246 JalistoProperties state = (JalistoProperties) physical.readFileObjectAt(SessionImpl.BSTATE_IFA).getData(); 247 Iterator propIt = state.getKeys().iterator(); 248 writer.println(tab + "State of base :"); 249 while (propIt.hasNext()) { 250 String key = (String ) 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 writer; 260 261 262 private static String propFilePath; 263 private static String 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 FILE_OPTION = "-file"; 269 private static final String INSTANCE_OPTION = "-i"; 270 private static final String NO_INSTANCE_OPTION = "-ni"; 271 private static final String META_OPTION = "-m"; 272 private static final String STATE_OPTION = "-s"; 273 private static final String HELP_OPTION = "-h"; 274 275 } 276 | Popular Tags |