The easy Weblog and Wiki Software
[ start | index | login ]
start > SnipSnap Export

SnipSnap Export

Created by leo. Last edited by leo, 2 years and 47 days ago. Viewed 3,145 times. #2
[diff] [history] [edit] [rdf]
labels
Category:snipsnap-documentation
attachments
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)."); } }

4 comments (by hp_, suxx0011, paularmstrong) | post comment

What is SnipSnap?
SnipSnap is a free and easy to install weblog and wiki tool written in Java.

SnipSnap download
Current version: 1.0b3-uttoxeter
Try our >>Web Start Demo!

Resources

5567 Users and 13713 Snips. Installed 6 years and 42 days ago

Logged in Users: (2)
… and 15 Guests.

snipsnap-changed for older changes.

< August 2008 >
SunMonTueWedThuFriSat
12
3456789
10111213141516
17181920212223
24252627282930
31

snipsnap
Listed on BlogShares
XHTML 1.0 validated
CSS validated
RSS 2.0 validated
RSS Feed

pico-powered

Powered by SnipSnap 1.0b3-uttoxeter
YourKit >>Java Profiler

Fraunhofer FIRST

snipsnap.org | Copyright 2000-2006 Fraunhofer FIRST