コード例 #1
0
public class ServiceManagerImpl implements ServiceManager, FrameworkConstants {

  private static final String APPTYPES_FILE = "apptypes.json";
  private static final String SETTINGS_FILE = "settings.json";
  private static final String VIDEOS_FILE = "videos.json";

  private static final Logger S_LOGGER = Logger.getLogger(ServiceManagerImpl.class);
  private static Boolean debugEnabled = S_LOGGER.isDebugEnabled();
  private Map<String, Technology> technologies = null;

  private Gson gson = null;
  private ServiceManagerCache cache = ServiceManagerCache.getInstance();

  public ServiceManagerImpl() throws PhrescoException {
    super();
    gson = new Gson();
    initCache();
  }

  private void initCache() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.initCache()");
    }
    List<ApplicationType> apptypes = null;
    List<VideoInfo> videoInfos = null;
    List<SettingsTemplate> settingsTemplate = null;
    List<Environment> enveInfo = null;

    if (PhrescoFrameworkFactory.getFrameworkConfig().isCacheEnabled()) {
      apptypes = loadAppTypesFromFile();
      videoInfos = loadVideosFromFile();
      settingsTemplate = loadSettingsTemplateFromFile();
    }

    // Load AppTypes into Cache
    if (apptypes == null) {
      apptypes = getApplicationTypesFromServer();
    }

    // Load Videos into Cache
    if (videoInfos == null) {
      videoInfos = loadVideosFromService();
    }

    // Load Settings into Cache
    if (settingsTemplate == null) {
      settingsTemplate = loadSettingsTemplateFromService();
    }

    if (enveInfo == null) {
      enveInfo = getEnvInfoFromService();
    }

    if (PhrescoFrameworkFactory.getFrameworkConfig().isCacheEnabled()) {
      writeToFile(VIDEOS_FILE, videoInfos);
      writeToFile(APPTYPES_FILE, apptypes);
      writeToFile(SETTINGS_FILE, settingsTemplate);
    }

    cache.add(APPTYPES_FILE, apptypes);
    cache.add(VIDEOS_FILE, videoInfos);
    cache.add(SETTINGS_FILE, settingsTemplate);
  }

  private List<SettingsTemplate> loadSettingsTemplateFromService() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.loadSettingsTemplateFromService()");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration frameworkConfig = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource = client.resource(frameworkConfig.getServerPath() + REST_SETTINGS_PATH);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE);

    GenericType<List<SettingsTemplate>> genericType = new GenericType<List<SettingsTemplate>>() {};
    return builder.get(genericType);
  }

  public List<Environment> getEnvInfoFromService() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getEnvFromService()");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration frameworkConfig = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource = client.resource(frameworkConfig.getServerPath() + REST_ENVE_PATH);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE);

    GenericType<List<Environment>> genericType = new GenericType<List<Environment>>() {};
    return builder.get(genericType);
  }

  private List<VideoInfo> loadVideosFromService() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.loadVideosFromService()");
    }
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    Client client = ClientHelper.createClient();
    WebResource resource = client.resource(configuration.getServerPath() + REST_VIDEOS_PATH);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE);
    GenericType<List<VideoInfo>> genericType = new GenericType<List<VideoInfo>>() {};
    return builder.get(genericType);
  }

  public List<DownloadInfo> getDownloadsFromService(DownloadPropertyInfo downloadPropertyInfo)
      throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.loadVideosFromService()");
    }
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    Client client = ClientHelper.createClient();
    WebResource resource = client.resource(configuration.getServerPath() + REST_DOWNLOADS_PATH);
    ClientResponse response =
        resource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .post(ClientResponse.class, downloadPropertyInfo);
    GenericType<List<DownloadInfo>> genericType = new GenericType<List<DownloadInfo>>() {};
    List<DownloadInfo> downloadInfo = response.getEntity(genericType);
    return downloadInfo;
  }

  public List<Server> getServers() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getServers()");
    }
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    Client client = ClientHelper.createClient();
    WebResource resource = client.resource(configuration.getServerPath() + REST_SERVERS_PATH);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE);
    GenericType<List<Server>> genericType = new GenericType<List<Server>>() {};
    return builder.get(genericType);
  }

  public List<Database> getDatabases() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getServers()");
    }
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    Client client = ClientHelper.createClient();
    WebResource resource = client.resource(configuration.getServerPath() + REST_DATABASE_PATH);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE);
    GenericType<List<Database>> genericType = new GenericType<List<Database>>() {};
    return builder.get(genericType);
  }

  private void writeToFile(String fileName, List jsonObjects) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug(
          "Entering Method ServiceManagerImpl.writeToFile(String fileName, List jsonObjects)");
    }
    String settingsInfoJson = gson.toJson(jsonObjects);
    FileWriter writer = null;

    try {
      if (debugEnabled) {
        S_LOGGER.debug("writeToFile() file Name = " + fileName);
      }
      File file = new File(Utility.getPhrescoTemp(), fileName);
      writer = new FileWriter(file);
      writer.write(settingsInfoJson);
      writer.flush();
    } catch (IOException e) {
      throw new PhrescoException(e);
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (IOException e) {
          throw new PhrescoException(e);
        }
      }
    }
  }

  private List<ApplicationType> getApplicationTypesFromServer() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getApplicationTypesFromServer()");
    }
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    Client client = ClientHelper.createClient();
    WebResource resource = client.resource(configuration.getServerPath() + REST_APPS_PATH);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE);
    GenericType<List<ApplicationType>> genericType = new GenericType<List<ApplicationType>>() {};
    return builder.get(genericType);
  }

  private List<ApplicationType> loadAppTypesFromFile() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.loadAppTypesFromFile()");
    }
    File file = new File(Utility.getPhrescoTemp(), APPTYPES_FILE);
    if (!file.exists()) {
      return null;
    }

    FileReader reader = null;
    try {
      reader = new FileReader(file);
      BufferedReader bufferedReader = new BufferedReader(reader);
      Type type = new TypeToken<List<ApplicationType>>() {}.getType();
      return gson.fromJson(bufferedReader, type);
    } catch (FileNotFoundException e) {
      throw new PhrescoException(e);
    } finally {
      if (reader != null)
        try {
          reader.close();
        } catch (IOException e) {
          throw new PhrescoException(e);
        }
    }
  }

  private List<VideoInfo> loadVideosFromFile() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.loadVideosFromFile()");
    }
    File file = new File(Utility.getPhrescoTemp(), VIDEOS_FILE);
    if (!file.exists()) {
      return null;
    }

    FileReader reader = null;
    try {
      reader = new FileReader(file);
      BufferedReader bufferedReader = new BufferedReader(reader);
      Type type = new TypeToken<List<VideoInfo>>() {}.getType();
      return gson.fromJson(bufferedReader, type);
    } catch (FileNotFoundException e) {
      throw new PhrescoException(e);
    } finally {
      if (reader != null)
        try {
          reader.close();
        } catch (IOException e) {
          throw new PhrescoException(e);
        }
    }
  }

  private List<SettingsTemplate> loadSettingsTemplateFromFile() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.loadSettingsTemplateFromFile()");
    }
    File file = new File(Utility.getPhrescoTemp(), SETTINGS_FILE);
    if (!file.exists()) {
      return null;
    }

    FileReader reader = null;
    try {
      if (debugEnabled) {
        S_LOGGER.debug("loadSettingsTemplateFromFile() file path = " + file.getPath());
      }
      reader = new FileReader(file);
      BufferedReader bufferedReader = new BufferedReader(reader);
      Type type = new TypeToken<List<SettingsTemplate>>() {}.getType();
      return gson.fromJson(bufferedReader, type);
    } catch (FileNotFoundException e) {
      throw new PhrescoException(e);
    } finally {
      if (reader != null)
        try {
          reader.close();
        } catch (IOException e) {
          throw new PhrescoException(e);
        }
    }
  }

  @SuppressWarnings("unchecked")
  public List<ApplicationType> getApplicationTypes() throws PhrescoException {
    return (List<ApplicationType>) cache.get(APPTYPES_FILE);
  }

  public ApplicationType getApplicationType(String name) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getApplicationType(String name)");
    }
    if (debugEnabled) {
      S_LOGGER.debug("getApplicationType() Name = " + name);
    }
    List<ApplicationType> applicationTypes = getApplicationTypes();
    for (ApplicationType applicationType : applicationTypes) {
      if (applicationType.getName().equals(name)) {
        return applicationType;
      }
    }

    return null;
  }

  public Map<String, Technology> getAllTechnologies() throws PhrescoException {
    if (technologies == null) {
      technologies = new HashMap<String, Technology>(32);
      fillMapTechnologies();
    }

    return technologies;
  }

  private void fillMapTechnologies() throws PhrescoException {
    List<ApplicationType> applicationTypes = getApplicationTypes();
    for (ApplicationType applicationType : applicationTypes) {
      List<Technology> appTechnologies = applicationType.getTechnologies();
      for (Technology technology : appTechnologies) {
        technologies.put(technology.getId(), technology);
      }
    }
  }

  @SuppressWarnings("unchecked")
  public List<SettingsTemplate> getSettingsTemplates() throws PhrescoException {
    return (List<SettingsTemplate>) cache.get(SETTINGS_FILE);
  }

  @SuppressWarnings("unchecked")
  public List<VideoInfo> getVideoInfos() throws PhrescoException {
    return (List<VideoInfo>) cache.get(VIDEOS_FILE);
  }

  public ClientResponse createProject(ProjectInfo info, User userInfo) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.createProject(ProjectInfo info)");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_APPS_PATH);
    resource.accept(MediaType.APPLICATION_OCTET_STREAM);
    if (debugEnabled) {
      S_LOGGER.debug("createProject() ProjectName = " + info.getName());
    }
    ClientResponse response =
        resource
            .header(Constants.AUTH_TOKEN, userInfo.getToken())
            .type(MediaType.APPLICATION_JSON)
            .post(ClientResponse.class, info);
    //        reSetCacheAppTypes();
    return response;
  }

  public ClientResponse updateProject(ProjectInfo info, User userInfo) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.updateProject(ProjectInfo info)");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_APPS_UPDATE_PATH);
    resource.accept(MediaType.APPLICATION_OCTET_STREAM);
    if (debugEnabled) {
      S_LOGGER.debug("updateProject() ProjectName = " + info.getName());
    }
    ClientResponse response =
        resource
            .header(Constants.AUTH_TOKEN, userInfo.getToken())
            .type(MediaType.APPLICATION_JSON)
            .post(ClientResponse.class, info);
    return response;
  }

  public ClientResponse updateDocumentProject(ProjectInfo info) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.updateProject(ProjectInfo info)");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(
            configuration.getServerPath() + FrameworkConstants.REST_APPS_UPDATEDOC_PATH);
    resource.accept(MediaType.APPLICATION_OCTET_STREAM);
    if (debugEnabled) {
      S_LOGGER.debug("updateDocumentProject() ProjectName = " + info.getName());
    }
    ClientResponse response =
        resource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, info);
    return response;
  }

  public User doLogin(Credentials credentials) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.doLogin(Credentials credentials)");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_LOGIN_PATH);
    resource.accept(MediaType.APPLICATION_JSON_TYPE);
    ClientResponse response =
        resource.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, credentials);
    GenericType<User> genericType = new GenericType<User>() {};
    User userInfo = response.getEntity(genericType);
    return userInfo;
  }

  @Override
  public List<AdminConfigInfo> getAdminConfig() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getAdminConfig()");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_ADMIN_CONFIG_PATH);
    resource.accept(MediaType.APPLICATION_JSON_TYPE);
    ClientResponse clientResponse =
        resource.type(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
    GenericType<List<AdminConfigInfo>> genericType = new GenericType<List<AdminConfigInfo>>() {};
    return clientResponse.getEntity(genericType);
  }

  @Override
  public String getCiConfigPath() throws PhrescoException {
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CI_CONFIG_PATH);
    return resource.accept(MediaType.TEXT_PLAIN).get(String.class);
  }

  @Override
  public String getCiSvnPath() throws PhrescoException {
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CI_SVN_PATH);
    return resource.accept(MediaType.TEXT_PLAIN).get(String.class);
  }

  @Override
  public ClientResponse sendReport(LogInfo loginfo) throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.sendReport(LogInfo loginfo)");
    }
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_LOG_PATH);
    ClientResponse response =
        resource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, loginfo);
    return response;
  }

  public InputStream getCredentialXml() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getCredentialXml");
    }

    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CREDENTIAL_PATH);
    resource.accept(MediaType.APPLICATION_XML);
    ClientResponse response = resource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
    InputStream is = response.getEntityInputStream();
    return is;
  }

  public InputStream getJdkHomeXml() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getJdkHomeXml");
    }

    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CI_JDK_HOME);
    resource.accept(MediaType.APPLICATION_XML);
    ClientResponse response = resource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
    InputStream is = response.getEntityInputStream();
    return is;
  }

  public InputStream getMavenHomeXml() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getCredentialXml");
    }

    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CI_MAVEN_HOME);
    resource.accept(MediaType.APPLICATION_XML);
    ClientResponse response = resource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
    InputStream is = response.getEntityInputStream();
    return is;
  }

  public InputStream getMailerXml() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getMailerXml");
    }

    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CI_MAILER_HOME);
    resource.accept(MediaType.APPLICATION_XML);
    ClientResponse response = resource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
    InputStream is = response.getEntityInputStream();
    return is;
  }

  public ClientResponse getEmailExtPlugin() throws PhrescoException {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method ServiceManagerImpl.getEmailExtPlugin");
    }

    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(configuration.getServerPath() + FrameworkConstants.REST_CI_MAIL_PLUGIN);
    resource.accept(MediaType.APPLICATION_OCTET_STREAM);
    ClientResponse response = resource.get(ClientResponse.class);
    return response;
  }

  // To reset the AppTypes in cache
  public void reSetCacheAppTypes() throws PhrescoException {
    ServiceManagerCache.getInstance().remove(APPTYPES_FILE);
    initCache();
  }

  @Override
  public List<Reports> getReports(String techId) throws PhrescoException {
    Client client = ClientHelper.createClient();
    FrameworkConfiguration configuration = PhrescoFrameworkFactory.getFrameworkConfig();
    WebResource resource =
        client.resource(
            configuration.getServerPath() + FrameworkConstants.REST_REPORTS + "/" + techId);
    Builder builder = resource.accept(MediaType.APPLICATION_JSON);
    GenericType<List<Reports>> genericType = new GenericType<List<Reports>>() {};
    List<Reports> reportList = builder.get(genericType);
    return reportList;
  }
}
コード例 #2
0
 // To reset the AppTypes in cache
 public void reSetCacheAppTypes() throws PhrescoException {
   ServiceManagerCache.getInstance().remove(APPTYPES_FILE);
   initCache();
 }