001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.core.plugins;
021
022import com.google.common.collect.Lists;
023import org.apache.commons.lang.StringUtils;
024
025import java.io.File;
026import java.util.List;
027
028public class RemotePlugin {
029  private String pluginKey;
030  private List<String> filenames = Lists.newArrayList();
031  private boolean core;
032
033  public RemotePlugin(String pluginKey, boolean core) {
034    this.pluginKey = pluginKey;
035    this.core = core;
036  }
037
038  public static RemotePlugin create(DefaultPluginMetadata metadata) {
039    RemotePlugin result = new RemotePlugin(metadata.getKey(), metadata.isCore());
040    result.addFilename(metadata.getFile().getName());
041    for (File file : metadata.getDeprecatedExtensions()) {
042      result.addFilename(file.getName());
043    }
044    return result;
045  }
046
047  public static RemotePlugin unmarshal(String row) {
048    String[] fields = StringUtils.split(row, ",");
049    RemotePlugin result = new RemotePlugin(fields[0], Boolean.parseBoolean(fields[1]));
050    if (fields.length > 2) {
051      for (int index = 2; index < fields.length; index++) {
052        result.addFilename(fields[index]);
053      }
054    }
055    return result;
056  }
057
058  public String marshal() {
059    StringBuilder sb = new StringBuilder();
060    sb.append(pluginKey).append(",");
061    sb.append(String.valueOf(core));
062    for (String filename : filenames) {
063      sb.append(",").append(filename);
064    }
065    return sb.toString();
066  }
067
068  public String getKey() {
069    return pluginKey;
070  }
071
072
073  public boolean isCore() {
074    return core;
075  }
076
077  public RemotePlugin addFilename(String s) {
078    filenames.add(s);
079    return this;
080  }
081
082  public List<String> getFilenames() {
083    return filenames;
084  }
085
086  public String getPluginFilename() {
087    return (!filenames.isEmpty() ? filenames.get(0) : null);
088  }
089
090  @Override
091  public boolean equals(Object o) {
092    if (this == o) {
093      return true;
094    }
095    if (o == null || getClass() != o.getClass()) {
096      return false;
097    }
098    RemotePlugin that = (RemotePlugin) o;
099    return pluginKey.equals(that.pluginKey);
100  }
101
102  @Override
103  public int hashCode() {
104    return pluginKey.hashCode();
105  }
106}