![]() | ![]() |
![]() |
| Project | Ver 1.1 | Ver 1.2 | JDO | JPA | Guides | Tools |
| 1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer |
JPOX Shell extends Jython to incorporates JPOX tools to facilitate development, administration and automation of tasks for management of JDO aware applications using scripts.
JPOX Shell requires the JPOX Core, JPOX Shell and its dependencies to run. Optionally, the JPOX Enhancer is required if enhancement is invoked within JPOX Shell. All libraries must be added to the classpath. The classpath can be set in the environment variable CLASSPATH. Start JPOX Shell by lauching jpoxshell.bat under Windows or jpoxshell.sh under Linux. D:\jpox>jpoxshell Starting JPOX Shell Interpreter... Type 'help()' for command list. >>>
JPOX Shell interpreter evaluates instructions either interactively or in batch mode. In interactive mode, JPOX Shell will evaluate every command at the time it is provided. In batch mode, JPOX Shell will load a script file (.py) and interprete all commands. To start JPOX Shell in batch mode, you run the jpoxshell command followed by the script file name (sample.py), and to start in interactive mode, you simply run the jpoxshell command.
The below script exemplifies the enhancement and persistence of two objects that are queried and printed to the console.
#the JDO, JPOX Core, JPOX Enhancer, JPOX Shell and Jython libraries are in the classpath
#we set an additional classpath to load persistent classes and database drivers
classPath(['/home/user/classes','/home/lib/derby.jar'])
#enhance
enhance(['/home/user/classes/org/test/package.jdo'])
#imports
import java
from java.io import File
from javax.jdo import JDOHelper
#create a PMF and obtain a PersistenceManager
props = File("/home/PMFProperties.properties")
pmf = JDOHelper.getPersistenceManagerFactory(props)
pm = pmf.getPersistenceManager()
#create some persistent objects
from org.jpox.samples.company import Person
erik = Person(1,'Erik','Bengtson','erik@jpox.org')
andy = Person(2,'Andy','Jefferson','andy@jpox.org')
pm.currentTransaction().begin()
pm.makePersistent(erik)
pm.makePersistent(andy)
pm.currentTransaction().commit()
#query persistent objects
pm.currentTransaction().begin()
q = pm.newQuery(Person)
q.setResult("firstName,lastName")
result = q.execute()
#loop though results
print "First loop"
for r in result:
print "first name:", r[0], "last name:", r[1]
#loop though results (another form)
from java.lang import System
System.out.println("Second loop")
for r in result:
System.out.println("first name: "+r[0]+" last name: "+r[1])
#release resources
pm.currentTransaction().commit()
pm.close()
pmf.close()
The console have the below ouput after running the above script. First loop first name: Andy last name: Jefferson first name: Erik last name: Bengtson Second loop first name: Andy last name: Jefferson first name: Erik last name: Bengtson
The below script exemplifies the execution of an SQL query where results are printed to the console.
#the JDO, JPOX Core, JPOX Enhancer, JPOX Shell and Jython libraries are in the classpath
#we set an additional classpath to load persistent classes and database drivers
classPath(['/home/user/classes','/home/lib/derby.jar'])
#imports
import java
#configuration
loadConfiguration('/home/user/pmf.properties')
#run sql
result = sql('SELECT * from PERSON') #result is printed to output
# iterating results and printing
print 'printing results for 2nd time.'
for r in result:
java.lang.System.out.print('Name: ')
for elm in r:
java.lang.System.out.print(elm)
java.lang.System.out.print(' ')
java.lang.System.out.println('')
The console have the below ouput after running the above script. Andy Jefferson Erik Bengtson print 'printing results for 2nd time.' Name: Andy Jefferson Name: Erik Bengtson
JPOX Shell defines custom functions besides those that exists in Java and Jython. These functions are to facilitate the usage of JPOX. Below a list of functions: loadConfigurationThis function loads the configuration for the database. Syntaxvoid loadConfiguration(propertiesFile)
Examples
loadConfiguration('/home/user/pmf.properties')
dbInfoThis function displays the database information. You must have invoked loadConfiguration function before invoking dbInfo. Syntaxvoid dbInfo() Examples
loadConfiguration('/home/user/pmf.properties')
dbInfo()
schemaInfoThis function displays the schema information. You must have invoked loadConfiguration function before invoking schemaInfo Syntaxvoid schemaInfo() Examples
loadConfiguration('/home/user/pmf.properties')
schemaInfo()
getPersistenceManagerAccessor for the PersistenceManager. You must have invoked loadConfiguration function before invoking getPersistenceManager SyntaxPersistenceManager getPersistenceManager() Examples
loadConfiguration('/home/user/pmf.properties')
pm = getPersistenceManager()
sqlExecute a SQL statement. You must have invoked loadConfiguration function before invoking sql SyntaxList sql(sql)
Examples
loadConfiguration('/home/user/pmf.properties')
result = sql('SELECT * from TABLE') #result is printed to output and returned
helpThis function displays a help list. Syntaxvoid help(name)
Examples
help()
help('dbInfo')
exitExits to the operating system. Syntaxvoid exit() Examples
exit() classPathThis function sets a classpath for loading persistent classes, metadata files, dadtabase drivers. Syntaxvoid classPath(files)
Examples
classPath(['file1.jar','/home/file2.jar','/dir']) enhanceThis function enhances persistent classes. The JPOX Enhancer and its dependencies must be in the classpath Syntaxvoid enhance(files)
Examples
enhance(['/home/user/package.jdo','/home/user/test.jdo']) enhance(['/home/user/Person.class','/home/user/Manager.class']) |