Skip to content

Commit

Permalink
Fix code style in couchbase log appender
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Oct 7, 2016
1 parent 660108d commit 7b5a92a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 64 deletions.
Expand Up @@ -13,43 +13,58 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */

package org.kaaproject.kaa.server.appenders.couchbase.appender; package org.kaaproject.kaa.server.appenders.couchbase.appender;


import com.couchbase.client.java.CouchbaseCluster; import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.cluster.ClusterInfo; import com.couchbase.client.java.cluster.ClusterInfo;
import com.couchbase.client.java.env.CouchbaseEnvironment; import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.core.CouchbaseTemplate;


import java.util.List; import java.util.List;


public class KaaCouchbaseCluster { public class KaaCouchbaseCluster {


private final List<String> bootstrapHosts; private final List<String> bootstrapHosts;
private final String bucketName; private final String bucketName;
private final String bucketPassword; private final String bucketPassword;
private CouchbaseEnvironment environment; private CouchbaseEnvironment environment;
private CouchbaseCluster cluster; private CouchbaseCluster cluster;
private ClusterInfo clusterInfo; private ClusterInfo clusterInfo;


public KaaCouchbaseCluster(List<String> bootstrapHosts, String bucketName, String bucketPassword) { /**
this.bootstrapHosts = bootstrapHosts; * Instantiates a new Kaa couchbase cluster.
this.bucketName = bucketName; *
this.bucketPassword = bucketPassword; * @param bootstrapHosts the bootstrap hosts
} * @param bucketName the bucket name

* @param bucketPassword the bucket password
public CouchbaseTemplate connect() throws Exception { */
environment = DefaultCouchbaseEnvironment.create(); public KaaCouchbaseCluster(List<String> bootstrapHosts, String bucketName,
cluster = CouchbaseCluster.create(environment, bootstrapHosts); String bucketPassword) {
clusterInfo = cluster.clusterManager(bucketName, bucketPassword).info(); this.bootstrapHosts = bootstrapHosts;
return new CouchbaseTemplate( this.bucketName = bucketName;
clusterInfo, this.bucketPassword = bucketPassword;
cluster.openBucket(bucketName, bucketPassword)); }
}

/**
public void disconnect() throws Exception { * Create couchbase template.
cluster.disconnect(); *
environment.shutdownAsync(); * @return the couchbase template
} * @throws Exception the exception
*/
public CouchbaseTemplate connect() throws Exception {
environment = DefaultCouchbaseEnvironment.create();
cluster = CouchbaseCluster.create(environment, bootstrapHosts);
clusterInfo = cluster.clusterManager(bucketName, bucketPassword).info();
return new CouchbaseTemplate(
clusterInfo,
cluster.openBucket(bucketName, bucketPassword));
}

public void disconnect() throws Exception {
cluster.disconnect();
environment.shutdownAsync();
}
} }
Expand Up @@ -32,51 +32,59 @@


public class LogEventCouchbaseDao implements LogEventDao { public class LogEventCouchbaseDao implements LogEventDao {


private static final Logger LOG = LoggerFactory.getLogger(LogEventCouchbaseDao.class); private static final Logger LOG = LoggerFactory.getLogger(LogEventCouchbaseDao.class);


private final Random RANDOM = new Random(); private final Random random = new Random();


private KaaCouchbaseCluster couchbaseConfiguration; private KaaCouchbaseCluster couchbaseConfiguration;
private CouchbaseTemplate couchbaseTemplate; private CouchbaseTemplate couchbaseTemplate;


public LogEventCouchbaseDao(CouchbaseConfig configuration) throws Exception { /**
couchbaseConfiguration = new KaaCouchbaseCluster( * Instantiates a new Log event couchbase dao.
configuration.getCouchbaseServerUris().stream().map(v -> v.getServerUri()).collect(Collectors.toList()), *
configuration.getBucket(), * @param configuration the configuration
configuration.getPassword()); * @throws Exception the exception
couchbaseTemplate = couchbaseConfiguration.connect(); */
couchbaseTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION); public LogEventCouchbaseDao(CouchbaseConfig configuration) throws Exception {
} couchbaseConfiguration = new KaaCouchbaseCluster(
configuration.getCouchbaseServerUris().stream()
.map(v -> v.getServerUri())
.collect(Collectors.toList()),
configuration.getBucket(),
configuration.getPassword());
couchbaseTemplate = couchbaseConfiguration.connect();
couchbaseTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
}


@Override @Override
public List<LogEvent> save(RecordHeader recordHeader, List<LogEventDto> logEventDtos) { public List<LogEvent> save(RecordHeader recordHeader, List<LogEventDto> logEventDtos) {
List<LogEvent> logEvents = new ArrayList<>(logEventDtos.size()); List<LogEvent> logEvents = new ArrayList<>(logEventDtos.size());
for (LogEventDto logEventDto : logEventDtos) { for (LogEventDto logEventDto : logEventDtos) {
LogEvent logEvent = new LogEvent(recordHeader, logEventDto); LogEvent logEvent = new LogEvent(recordHeader, logEventDto);
logEvent.setId(getId(logEventDto.getId())); logEvent.setId(getId(logEventDto.getId()));
logEvents.add(logEvent); logEvents.add(logEvent);
}
LOG.debug("Saving {} log events", logEvents.size());
couchbaseTemplate.insert(logEvents);
return logEvents;
} }
LOG.debug("Saving {} log events", logEvents.size());
couchbaseTemplate.insert(logEvents);
return logEvents;
}


@Override @Override
public void close() { public void close() {
if (couchbaseConfiguration != null) { if (couchbaseConfiguration != null) {
try { try {
couchbaseConfiguration.disconnect(); couchbaseConfiguration.disconnect();
} catch (Exception e) { } catch (Exception ex) {
LOG.error("Failed to disconnect from couchbase cluster!", e); LOG.error("Failed to disconnect from couchbase cluster!", ex);
} }
}
} }
}


private String getId(String id) { private String getId(String id) {
if (id == null || id.length() == 0) { if (id == null || id.length() == 0) {
id = new UUID(System.currentTimeMillis(), RANDOM.nextLong()).toString(); id = new UUID(System.currentTimeMillis(), random.nextLong()).toString();
}
return id;
} }
return id;
}


} }
Expand Up @@ -3,6 +3,7 @@
* *
* DO NOT EDIT DIRECTLY * DO NOT EDIT DIRECTLY
*/ */

package org.kaaproject.kaa.server.appenders.couchbase.config.gen; package org.kaaproject.kaa.server.appenders.couchbase.config.gen;
@SuppressWarnings("all") @SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated @org.apache.avro.specific.AvroGenerated
Expand Down

0 comments on commit 7b5a92a

Please sign in to comment.