Search in sources :

Example 1 with MongoInstance

use of org.mongodb.meclipse.preferences.MongoInstance in project meclipse by flaper87.

the class Connection method getMongo.

// public TreeObject [] getChildren() {
// loadDatabases();
// return super.getChildren();
// }
/**
	 * In the style of lazy-loading, this is where we actually initiate the
	 * connection to a MongoDB instance - and this is where a user would 1st
	 * request to see data obtained via the connection.
	 * 
	 * @return
	 */
public Mongo getMongo() {
    MongoInstance mongoInstance = MeclipsePlugin.getDefault().getMongoInstance(this.getName());
    Exception ex;
    if (mongoInstance.getMongo() == null) {
        Mongo mongo;
        try {
            mongo = new Mongo(mongoInstance.getHost(), mongoInstance.getPort());
            mongo.getDatabaseNames();
            // add the active Mongo instance
            mongoInstance.setMongo(mongo);
            // to the plug-in's state
            isDown = false;
            return mongo;
        /* catch some possible exceptions */
        } catch (MongoException e) {
            ex = e;
        } catch (UnknownHostException e) {
            ex = e;
        }
        if (!isDown) {
            this.showMessage(String.format(getCaption("connection.connectionError"), this.getName(), mongoInstance.getHost(), ex));
            isDown = true;
        }
        return null;
    } else {
        return mongoInstance.getMongo();
    }
}
Also used : MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) Mongo(com.mongodb.Mongo) MongoInstance(org.mongodb.meclipse.preferences.MongoInstance) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException)

Example 2 with MongoInstance

use of org.mongodb.meclipse.preferences.MongoInstance in project meclipse by flaper87.

the class MeclipsePlugin method loadSavedServers.

private MongoInstance[] loadSavedServers() {
    CsvReader reader = null;
    try {
        IPath libPath = MeclipsePlugin.getDefault().getStateLocation();
        libPath = libPath.append("servers.cfg");
        File file = libPath.toFile();
        if (!file.exists())
            return new MongoInstance[0];
        reader = new CsvReader(new BufferedReader(new FileReader(file)));
        java.util.List<MongoInstance> savedServersList = new ArrayList<MongoInstance>();
        while (reader.readRecord()) {
            MongoInstance server = new MongoInstance(reader.get(0));
            server.setHost(reader.get(1));
            try {
                server.setPort(Integer.valueOf(reader.get(2)));
            } catch (NumberFormatException e) {
                System.out.println(e);
            }
            savedServersList.add(server);
        }
        return savedServersList.toArray(new MongoInstance[savedServersList.size()]);
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return new MongoInstance[0];
}
Also used : CsvReader(com.csvreader.CsvReader) IPath(org.eclipse.core.runtime.IPath) MongoInstance(org.mongodb.meclipse.preferences.MongoInstance) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 3 with MongoInstance

use of org.mongodb.meclipse.preferences.MongoInstance in project meclipse by flaper87.

the class MeclipsePlugin method start.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;
    MongoInstance[] savedServers = loadSavedServers();
    for (MongoInstance savedServer : savedServers) {
        mongoInstances.put(savedServer.getName(), savedServer);
    }
}
Also used : MongoInstance(org.mongodb.meclipse.preferences.MongoInstance)

Example 4 with MongoInstance

use of org.mongodb.meclipse.preferences.MongoInstance in project meclipse by flaper87.

the class MeclipsePlugin method saveServers.

private void saveServers() {
    // save server preferences here
    CsvWriter writer = null;
    try {
        IPath libPath = getStateLocation();
        libPath = libPath.append("servers.cfg");
        File file = libPath.toFile();
        if (!file.exists()) {
            file.createNewFile();
        }
        writer = new CsvWriter(new FileWriter(file, false), ',');
        for (MongoInstance server : mongoInstances.values()) {
            writer.write(server.getName());
            writer.write(server.getHost());
            writer.write(String.valueOf(server.getPort()));
            writer.endRecord();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : CsvWriter(com.csvreader.CsvWriter) IPath(org.eclipse.core.runtime.IPath) FileWriter(java.io.FileWriter) MongoInstance(org.mongodb.meclipse.preferences.MongoInstance) IOException(java.io.IOException) File(java.io.File)

Example 5 with MongoInstance

use of org.mongodb.meclipse.preferences.MongoInstance in project meclipse by flaper87.

the class ConnectionWizard method performFinish.

/**
	 * This method is called when 'Finish' button is pressed in the wizard. We
	 * will create an operation and run it using wizard as execution context.
	 */
public boolean performFinish() {
    // 1st, add the connection to our overall state:
    MongoInstance mongoInstance = new MongoInstance(page.getConnName());
    mongoInstance.setHost(page.getHost());
    mongoInstance.setPort(page.getPort());
    MeclipsePlugin.getDefault().addMongo(page.getConnName(), mongoInstance);
    return true;
}
Also used : MongoInstance(org.mongodb.meclipse.preferences.MongoInstance)

Aggregations

MongoInstance (org.mongodb.meclipse.preferences.MongoInstance)6 File (java.io.File)2 IOException (java.io.IOException)2 IPath (org.eclipse.core.runtime.IPath)2 CsvReader (com.csvreader.CsvReader)1 CsvWriter (com.csvreader.CsvWriter)1 Mongo (com.mongodb.Mongo)1 MongoException (com.mongodb.MongoException)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1