com.janeluo.jfinalplus.plugin.monogodb.MongodbPlugin Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2013, kidzhou 周磊 ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.janeluo.jfinalplus.plugin.monogodb;
import com.jfinal.log.Log;
import com.jfinal.plugin.IPlugin;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;
public class MongodbPlugin implements IPlugin {
private static final String DEFAULT_HOST = "127.0.0.1";
private static final int DEFAUL_PORT = 27017;
protected final Log logger = Log.getLog(getClass());
private MongoClient client;
private String host;
private int port;
private String database;
public MongodbPlugin(String database) {
this.host = DEFAULT_HOST;
this.port = DEFAUL_PORT;
this.database = database;
}
public MongodbPlugin(String host, int port, String database) {
this.host = host;
this.port = port;
this.database = database;
}
@Override
public boolean start() {
try {
client = new MongoClient(host, port);
} catch (UnknownHostException e) {
throw new RuntimeException("can't connect mongodb, please check the host and port:" + host + "," + port, e);
}
MongoKit.init(client, database);
return true;
}
@Override
public boolean stop() {
if (client != null) {
client.close();
}
return true;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy