Sometimes it might be important to get direct access to the SnipSpace. For our further development we want to decouple some of the components more, so I wrote this little test application which directly accesses the data storage and exports the database from the command line.
This is an example how to access a SnipSnap data store without the web GUI:
I have not yet optimized which libs to include in the classpath. The following command would access a default standalone installation (main instance) and dump all data (users and snips) into mydatabase.snip:
java -cp <snipsnap libs> Main applications/_8668_/webapp mydatabase.snip
Sources:
import org.snipsnap.app.Application;
import org.snipsnap.app.ApplicationManager;
import org.snipsnap.config.Configuration;
import org.snipsnap.config.ConfigurationManager;
import org.snipsnap.config.ConfigurationProxy;
import org.snipsnap.config.Globals;
import org.snipsnap.container.Components;
import org.snipsnap.snip.Snip;
import org.snipsnap.snip.SnipSpace;
import org.snipsnap.snip.XMLSnipExport;
import org.snipsnap.user.UserManager;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Iterator;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;/**
* @author Matthias L. Jugel <matthias.jugel@first.fraunhofer.de>
* @version $Id$
*/
public class Main {
public static void main(String[] args) {
File applicationPath = new File(args[0]); // initialize configuration (global db config)
System.err.println("-- INITIALIZING GLOBALS CONFIGURATION");
initialize(applicationPath);
// load all instances of snip spaces
System.err.println("-- LOADING AND CONFIGURING SNIP SPACES");
loadApplicationContexts(); // Hack: Application.getParameters() should always return a valid map (even empty)
Application.get().setParameters(new HashMap()); // output a snip as rendere HTML
System.err.println("-- EXPORTING DATABASE"); UserManager um = (UserManager) Components.getComponent(UserManager.class);
SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); List snips = space.getAll();
List users = um.getAll(); System.setProperty("file.encoding", "UTF-8");
System.err.println(Application.get().getConfiguration());
// Store data on System.out or to a file named on the command line (2nd argument)
OutputStream out = System.out;
if(args.length > 1) {
try {
out = new FileOutputStream(new File(args[1]));
} catch (FileNotFoundException e) {
System.err.println("!! Can't save to '"+args[1]+"': "+e.getMessage());
}
}
XMLSnipExport.store(out, snips, users,
// there is no matching for snips we want to include (include all)
null,
// ignore the "application" OID value
Arrays.asList(new String[] { "application" }),
// path to the file storage (attachments)
Application.get().getConfiguration().getFilePath()); System.err.println("The data has been exported in UTF-8 encoding!");
System.exit(0);
} /**
* Initialize the basic SnipSnap functionality. This loads the database configuration global
* for all SnipSpace instances.
* @param applicationPath the path to the web application directory
* @return the global configuration
*/
private static Globals initialize(File applicationPath) {
Globals globals = ConfigurationProxy.getInstance();
try {
globals.loadGlobals(new FileInputStream(new File(applicationPath, "WEB-INF/application.conf")));
} catch (Exception e) {
System.err.println("InitFilter: unable to load globals: " + applicationPath + ": " + e.getMessage());
} System.out.println(">> Installation key: " + globals.getInstallKey());
globals.setWebInfDir(new File(applicationPath, "WEB-INF"));
return globals;
}
/**
* Load all SnipSpace instances.
*/
private static void loadApplicationContexts() {
// get the application manager and all prefixes
ApplicationManager appManager = (ApplicationManager) Components.getComponent(ApplicationManager.class);
Collection prefixes = appManager.getPrefixes(); Iterator prefixIt = prefixes.iterator();
Application app = Application.get();
int okCount = 0; // Iterate through all prefixes (one per SnipSpace) and load configuration
while (prefixIt.hasNext()) {
String prefix = (String) prefixIt.next();
String appOid = appManager.getApplication(prefix);
app.storeObject(Application.OID, appOid); System.out.print(">> Loading: " + prefix + " ");
Configuration appConfig = ConfigurationProxy.newInstance();
SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
if (space.exists(Configuration.SNIPSNAP_CONFIG)) {
Snip configSnip = space.load(Configuration.SNIPSNAP_CONFIG);
String configContent = configSnip.getContent();
try {
appConfig.load(new ByteArrayInputStream(configContent.getBytes()));
okCount++;
System.out.print("(" + appConfig.getName() + ", " + appConfig.getUrl() + ")");
} catch (IOException e) {
System.out.print("ERROR: " + e.getMessage());
continue;
}
// add current configuration to the configuration manager
ConfigurationManager.getInstance().addConfiguration(appOid, appConfig);
} else {
System.out.print("(NOT CONFIGURED)");
}
System.out.println();
}
System.out.println(">> Loaded " + okCount + " instances (" + (prefixes.size() - okCount) + " not configured).");
}
}