001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 */ 017package org.nuxeo.connect.tools.report.config; 018 019import java.io.IOException; 020import java.io.OutputStream; 021import java.io.OutputStreamWriter; 022 023import javax.xml.bind.JAXBContext; 024import javax.xml.bind.JAXBException; 025import javax.xml.bind.Marshaller; 026import javax.xml.stream.XMLStreamWriter; 027 028import org.nuxeo.connect.identity.LogicalInstanceIdentifier.NoCLID; 029import org.nuxeo.connect.tools.report.ReportWriter; 030import org.nuxeo.connect.update.PackageException; 031import org.nuxeo.launcher.config.ConfigurationGenerator; 032import org.nuxeo.launcher.connect.ConnectBroker; 033import org.nuxeo.launcher.info.ConfigurationInfo; 034import org.nuxeo.launcher.info.DistributionInfo; 035import org.nuxeo.launcher.info.InstanceInfo; 036import org.nuxeo.launcher.info.KeyValueInfo; 037import org.nuxeo.launcher.info.PackageInfo; 038 039import com.sun.jersey.api.json.JSONConfiguration; 040import com.sun.jersey.json.impl.writer.JsonXmlStreamWriter; 041 042/** 043 * Write Runtime Configuration in json 044 * 045 * @since 8.4 046 */ 047public class ConfigReport implements ReportWriter { 048 049 @Override 050 public void write(OutputStream output) throws IOException { 051 try { 052 ConfigurationGenerator configurationGenerator = new ConfigurationGenerator(); 053 configurationGenerator.init(); 054 ConnectBroker connectBroker = new ConnectBroker(configurationGenerator.getEnv()); 055 InstanceInfo info = configurationGenerator.getServerConfigurator().getInfo(getCLID(connectBroker), 056 connectBroker.getPkgList()); 057 JAXBContext context = JAXBContext.newInstance(InstanceInfo.class, DistributionInfo.class, PackageInfo.class, 058 ConfigurationInfo.class, KeyValueInfo.class); 059 Marshaller marshaller = context.createMarshaller(); 060 marshaller.marshal(info, jsonWriter(context, output)); 061 } catch (PackageException | JAXBException cause) { 062 throw new IOException("Cannot write runtime configuration", cause); 063 } 064 } 065 066 protected String getCLID(ConnectBroker connectBroker) { 067 try { 068 return connectBroker.getCLID(); 069 } catch (NoCLID cause) { 070 return "no-clid"; 071 } 072 } 073 074 protected XMLStreamWriter jsonWriter(JAXBContext context, OutputStream out) { 075 JSONConfiguration config = JSONConfiguration.mapped().rootUnwrapping(true).attributeAsElement("key", "value").build(); 076 config = JSONConfiguration.createJSONConfigurationWithFormatted(config, true); 077 return JsonXmlStreamWriter.createWriter(new OutputStreamWriter(out), config, ""); 078 } 079 080}