Search in sources :

Example 11 with OJSONWriter

use of com.orientechnologies.orient.core.serialization.serializer.OJSONWriter in project orientdb by orientechnologies.

the class OServerCommandGetDatabase method exec.

protected void exec(final OHttpRequest iRequest, final OHttpResponse iResponse, final String[] urlParts) throws InterruptedException, IOException {
    ODatabaseDocumentInternal db = null;
    try {
        if (urlParts.length > 2) {
            db = server.getDatabasePoolFactory().get(urlParts[1], urlParts[2], urlParts[3]).acquire();
        } else
            db = getProfiledDatabaseInstance(iRequest);
        final StringWriter buffer = new StringWriter();
        final OJSONWriter json = new OJSONWriter(buffer);
        json.beginObject();
        json.beginObject("server");
        json.writeAttribute("version", OConstants.ORIENT_VERSION);
        if (OConstants.getBuildNumber() != null)
            json.writeAttribute("build", OConstants.getBuildNumber());
        json.writeAttribute("osName", System.getProperty("os.name"));
        json.writeAttribute("osVersion", System.getProperty("os.version"));
        json.writeAttribute("osArch", System.getProperty("os.arch"));
        json.writeAttribute("javaVendor", System.getProperty("java.vm.vendor"));
        json.writeAttribute("javaVersion", System.getProperty("java.vm.version"));
        json.endObject();
        if (((OMetadataInternal) db.getMetadata()).getImmutableSchemaSnapshot().getClasses() != null) {
            json.beginCollection("classes");
            List<String> classNames = new ArrayList<String>();
            for (OClass cls : db.getMetadata().getSchema().getClasses()) classNames.add(cls.getName());
            Collections.sort(classNames);
            for (String className : classNames) {
                final OClass cls = db.getMetadata().getSchema().getClass(className);
                try {
                    exportClass(db, json, cls);
                } catch (Exception e) {
                    OLogManager.instance().error(this, "Error on exporting class '" + cls + "'", e);
                }
            }
            json.endCollection();
        }
        if (db.getClusterNames() != null) {
            json.beginCollection("clusters");
            OCluster cluster;
            for (String clusterName : db.getClusterNames()) {
                try {
                    cluster = db.getStorage().getClusterById(db.getClusterIdByName(clusterName));
                } catch (IllegalArgumentException e) {
                    OLogManager.instance().error(this, "Cluster '%s' does not exist in database", clusterName);
                    continue;
                }
                try {
                    final String conflictStrategy = cluster.getRecordConflictStrategy() != null ? cluster.getRecordConflictStrategy().getName() : null;
                    json.beginObject();
                    json.writeAttribute("id", cluster.getId());
                    json.writeAttribute("name", clusterName);
                    json.writeAttribute("records", cluster.getEntries() - cluster.getTombstonesCount());
                    json.writeAttribute("conflictStrategy", conflictStrategy);
                    json.writeAttribute("size", "-");
                    json.writeAttribute("filled", "-");
                    json.writeAttribute("maxSize", "-");
                    json.writeAttribute("files", "-");
                } catch (Exception e) {
                    json.writeAttribute("records", "? (Unauthorized)");
                }
                json.endObject();
            }
            json.endCollection();
        }
        if (db.getUser() != null) {
            json.writeAttribute("currentUser", db.getUser().getName());
        // exportSecurityInfo(db, json);
        }
        final OIndexManagerProxy idxManager = db.getMetadata().getIndexManager();
        json.beginCollection("indexes");
        for (OIndex<?> index : idxManager.getIndexes()) {
            json.beginObject();
            try {
                json.writeAttribute("name", index.getName());
                json.writeAttribute("configuration", index.getConfiguration());
            // Exclude index size because it's too costly
            // json.writeAttribute("size", index.getSize());
            } catch (Exception e) {
                OLogManager.instance().error(this, "Cannot serialize index configuration", e);
            }
            json.endObject();
        }
        json.endCollection();
        json.beginObject("config");
        json.beginCollection("values");
        json.writeObjects(null, new Object[] { "name", "dateFormat", "value", db.getStorage().getConfiguration().dateFormat }, new Object[] { "name", "dateTimeFormat", "value", db.getStorage().getConfiguration().dateTimeFormat }, new Object[] { "name", "localeCountry", "value", db.getStorage().getConfiguration().getLocaleCountry() }, new Object[] { "name", "localeLanguage", "value", db.getStorage().getConfiguration().getLocaleLanguage() }, new Object[] { "name", "charSet", "value", db.getStorage().getConfiguration().getCharset() }, new Object[] { "name", "timezone", "value", db.getStorage().getConfiguration().getTimeZone().getID() }, new Object[] { "name", "definitionVersion", "value", db.getStorage().getConfiguration().version }, new Object[] { "name", "clusterSelection", "value", db.getStorage().getConfiguration().getClusterSelection() }, new Object[] { "name", "minimumClusters", "value", db.getStorage().getConfiguration().getMinimumClusters() }, new Object[] { "name", "conflictStrategy", "value", db.getStorage().getConfiguration().getConflictStrategy() });
        json.endCollection();
        json.beginCollection("properties");
        if (db.getStorage().getConfiguration().getProperties() != null)
            for (OStorageEntryConfiguration entry : db.getStorage().getConfiguration().getProperties()) {
                if (entry != null) {
                    json.beginObject();
                    json.writeAttribute("name", entry.name);
                    json.writeAttribute("value", entry.value);
                    json.endObject();
                }
            }
        json.endCollection();
        json.endObject();
        json.endObject();
        json.flush();
        iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
    } finally {
        if (db != null)
            db.close();
    }
}
Also used : OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) ArrayList(java.util.ArrayList) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OIndexManagerProxy(com.orientechnologies.orient.core.index.OIndexManagerProxy) StringWriter(java.io.StringWriter) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCluster(com.orientechnologies.orient.core.storage.OCluster) OStorageEntryConfiguration(com.orientechnologies.orient.core.config.OStorageEntryConfiguration)

Aggregations

OJSONWriter (com.orientechnologies.orient.core.serialization.serializer.OJSONWriter)11 StringWriter (java.io.StringWriter)9 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 IOException (java.io.IOException)4 OStorageEntryConfiguration (com.orientechnologies.orient.core.config.OStorageEntryConfiguration)2 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 OCluster (com.orientechnologies.orient.core.storage.OCluster)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1 OFetchPlan (com.orientechnologies.orient.core.fetch.OFetchPlan)1 OJSONFetchContext (com.orientechnologies.orient.core.fetch.json.OJSONFetchContext)1 OJSONFetchListener (com.orientechnologies.orient.core.fetch.json.OJSONFetchListener)1