From: Stephane Gamard Date: Wed, 30 Apr 2014 12:34:19 +0000 (+0200) Subject: SONAR-5237 - Created Base and Rule Normalizer classes X-Git-Tag: 4.4-RC1~1314 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=20be96b156cc0e9f787da9a1b188079487a79a6f;p=sonarqube.git SONAR-5237 - Created Base and Rule Normalizer classes --- diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java index 6a733d4e6c6..48ef50aaa79 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java @@ -20,15 +20,12 @@ package org.sonar.server.rule2; import com.google.common.collect.ImmutableSet; -import org.apache.commons.beanutils.BeanUtils; import org.elasticsearch.common.xcontent.XContentBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.rule.RuleKey; import org.sonar.core.cluster.WorkQueue; import org.sonar.core.profiling.Profiling; -import org.sonar.core.qualityprofile.db.ActiveRuleDao; -import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.rule.RuleConstants; import org.sonar.core.rule.RuleDto; import org.sonar.server.es.ESNode; @@ -37,9 +34,6 @@ import org.sonar.server.search.QueryOptions; import org.sonar.server.search.Results; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -51,11 +45,9 @@ public class RuleIndex extends BaseIndex { public static final Set PUBLIC_FIELDS = ImmutableSet.of("repositoryKey", "key", "name", "desc", "lang", "severity", "status", "tags", "sysTags", "createdAt", "updatedAt"); - private final ActiveRuleDao activeRuleDao; - - public RuleIndex(WorkQueue workQueue, RuleDao dao, ActiveRuleDao ActiveRuleDao, Profiling profiling, ESNode node) { - super(workQueue, dao, profiling, node); - this.activeRuleDao = ActiveRuleDao; + public RuleIndex(RuleNormalizer normalizer, WorkQueue workQueue, + Profiling profiling, ESNode node) { + super(normalizer, workQueue, profiling, node); } @Override @@ -125,88 +117,16 @@ public class RuleIndex extends BaseIndex { return mapping.endObject() .endObject().endObject(); - // return jsonBuilder().startObject() - // .startObject("issue") - // .startObject("properties") - // .startObject("component.path") - // .field("type", "string") - // .field("index_analyzer", "path_analyzer") - // .field("search_analyzer", "keyword") - // .endObject() - // .startObject("rule.name") - // .field("type", "string") - // .field("analyzer", "keyword") - // .endObject() - // .startObject("root.id") - // .field("type", "multi_field") - // .startObject("fields") - // .startObject("str") - // .field("type", "string") - // .field("index","analyzed") - // .field("analyzer", "default") - // .endObject() - // .startObject("num") - // .field("type", "long") - // .field("index","analyzed") - // .endObject() - // .endObject() - // .endObject() - // .endObject().endObject(); } catch (IOException e) { LOG.error("Could not create mapping for {}", this.getIndexName()); return null; } } - @Override - public XContentBuilder normalize(RuleKey key) { - - try { - - RuleDto rule = dao.getByKey(key); - - XContentBuilder document = jsonBuilder().startObject(); - - Map properties = BeanUtils.describe(rule); - - for (Entry property : properties.entrySet()) { - LOG.trace("NORMALIZING: {} -> {}", property.getKey(), property.getValue()); - document.field(property.getKey(), property.getValue()); - } - - document.startArray("active"); - for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) { - document.startObject(); - Map activeRuleProperties = BeanUtils.describe(activeRule); - for (Entry activeRuleProp : activeRuleProperties.entrySet()) { - LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue()); - document.field(activeRuleProp.getKey(), activeRuleProp.getValue()); - } - document.endObject(); - } - document.endArray(); - - return document.endObject(); - } catch (IOException e) { - LOG.error("Could not normalize {} in {} because {}", - key, this.getClass().getSimpleName(), e.getMessage()); - } catch (IllegalAccessException e) { - LOG.error("Could not normalize {} in {} because {}", - key, this.getClass().getSimpleName(), e.getMessage()); - } catch (InvocationTargetException e) { - LOG.error("Could not normalize {} in {} because {}", - key, this.getClass().getSimpleName(), e.getMessage()); - } catch (NoSuchMethodException e) { - LOG.error("Could not normalize {} in {} because {}", - key, this.getClass().getSimpleName(), e.getMessage()); - } - return null; - } - public Results search(RuleQuery query, QueryOptions options) { + throw new UnsupportedOperationException("TODO"); } - } diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java new file mode 100644 index 00000000000..050a453909b --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java @@ -0,0 +1,78 @@ +package org.sonar.server.rule2; + +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.sonar.api.rule.RuleKey; +import org.sonar.core.qualityprofile.db.ActiveRuleDao; +import org.sonar.core.rule.RuleDto; +import org.sonar.server.search.BaseNormalizer; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; + +public class RuleNormalizer extends BaseNormalizer { + + private RuleDao ruleDao; + private ActiveRuleDao activeRuleDao; + + public enum RuleFields { + KEY("key"), + RULE_KEY("ruleKey"), + REPOSITORY_KEY("repositoryKey"), + NAME("name"), + CREATED_AT("createdAt"); + + private final String key; + + private RuleFields(final String key) { + this.key = key; + } + + public String key() { + return key; + } + + public String toString() { + return key; + } + } + + public RuleNormalizer(RuleDao ruleDao, ActiveRuleDao activeRuleDao) { + this.ruleDao = ruleDao; + this.activeRuleDao = activeRuleDao; + } + + @Override + public XContentBuilder normalize(RuleKey key) throws IOException { + return normalize(ruleDao.getByKey(key)); + } + + @Override + public XContentBuilder normalize(RuleDto rule) throws IOException { + + XContentBuilder document = jsonBuilder().startObject(); + + indexField(RuleFields.KEY.key(), rule.getKey(), document); + indexField(RuleFields.NAME.key(), rule.getName(), document); + indexField(RuleFields.CREATED_AT.key(), rule.getCreatedAt(), document); + indexField(RuleFields.RULE_KEY.key(), rule.getRuleKey(), document); + indexField(RuleFields.REPOSITORY_KEY.key(), rule.getRepositoryKey(), document); + + // document.startArray("active"); + // for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) { + // document.startObject(); + // Map activeRuleProperties = BeanUtils.describe(activeRule); + // for (Entry activeRuleProp : activeRuleProperties.entrySet()) { + // LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue()); + // document.field(activeRuleProp.getKey(), activeRuleProp.getValue()); + // } + // document.endObject(); + // } + // document.endArray(); + + return document.endObject(); + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index c6e7bcf74c8..f7703c3894e 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -32,7 +32,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.core.cluster.WorkQueue; -import org.sonar.core.db.Dao; import org.sonar.core.db.Dto; import org.sonar.core.profiling.Profiling; import org.sonar.core.profiling.Profiling.Level; @@ -56,20 +55,15 @@ public abstract class BaseIndex> implem private final Profiling profiling; private Client client; private final ESNode node; - private IndexSynchronizer synchronizer; - protected Dao dao; + protected BaseNormalizer normalizer; - public BaseIndex(WorkQueue workQueue, Dao dao, Profiling profiling, ESNode node) { + public BaseIndex(BaseNormalizer normalizer, WorkQueue workQueue, + Profiling profiling, ESNode node) { + this.normalizer = normalizer; this.profiling = profiling; - this.synchronizer = new IndexSynchronizer(this, dao, workQueue); - this.dao = dao; this.node = node; } - protected Dao getDao() { - return this.dao; - } - protected Client getClient() { return this.client; } @@ -84,9 +78,6 @@ public abstract class BaseIndex> implem /* Setup the index if necessary */ this.intializeIndex(); - - /* Launch synchronization */ -// synchronizer.start(); } @Override @@ -102,25 +93,6 @@ public abstract class BaseIndex> implem public void connect() { this.client = this.node.client(); - // /* Settings to access our local ES node */ - // Settings settings = ImmutableSettings.settingsBuilder() - // .put("client.transport.sniff", true) - // .put("cluster.name", ES_CLUSTER_NAME) - // .put("node.name", "localclient_") - // .build(); - // - // this.client = new TransportClient(settings) - // .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT)); - // - // /* - // * Cannot do that yet, need version >= 1.0 - // * ImmutableList nodes = client.connectedNodes(); - // * if (nodes.isEmpty()) { - // * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!"); - // * } else { - // * log.info("connected to nodes: " + nodes.toString()); - // * } - // */ } /* Cluster And ES Stats/Client methods */ @@ -182,14 +154,14 @@ public abstract class BaseIndex> implem @Override public void insert(K key) { try { - XContentBuilder doc = this.normalize(key); + XContentBuilder doc = normalizer.normalize(key); String keyvalue = this.getKeyValue(key); if (doc != null && keyvalue != null && !keyvalue.isEmpty()) { LOG.debug("Update document with key {}", key); IndexResponse result = getClient().index( new IndexRequest(this.getIndexName(), this.getType(), keyvalue) - .source(this.normalize(key))).get(); + .source(doc)).get(); } else { LOG.error("Could not normalize document {} for insert in ", key, this.getIndexName()); @@ -204,10 +176,11 @@ public abstract class BaseIndex> implem public void update(K key) { try { LOG.info("Update document with key {}", key); + XContentBuilder doc = normalizer.normalize(key); UpdateResponse result = getClient().update( new UpdateRequest(this.getIndexName(), this.getType(), this.getKeyValue(key)) - .doc(this.normalize(key))).get(); + .doc(doc)).get(); } catch (Exception e) { LOG.error("Could not update documet for index {}: {}", this.getIndexName(), e.getMessage()); diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java new file mode 100644 index 00000000000..f092051ac32 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java @@ -0,0 +1,63 @@ +package org.sonar.server.search; + +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.core.db.Dto; + +import java.io.IOException; +import java.io.Serializable; + +public abstract class BaseNormalizer, K extends Serializable> { + + public abstract XContentBuilder normalize(K key) throws IOException; + + public abstract XContentBuilder normalize(E dto) throws IOException; + + private static final Logger LOG = LoggerFactory.getLogger(BaseNormalizer.class); + + protected void indexField(String field, Object value, XContentBuilder document){ + try { + document.field(field,value); + } catch (IOException e) { + LOG.error("Could not set {} to {} in ESDocument", field, value); + } + } + +// protected void indexField(Fields field, Object dto, XContentBuilder document) { +// try { +// document.field(field.key(), field.method.invoke(dto)); +// } catch (IllegalArgumentException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (IllegalAccessException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (InvocationTargetException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } +// +// +// +//private static Method getReadMethod(String method){ +// try { +// return RuleDto.class.getDeclaredMethod(method); +// } catch (SecurityException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (NoSuchMethodException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// return null; +//} + + + + +} diff --git a/sonar-server/src/main/java/org/sonar/server/search/Index.java b/sonar-server/src/main/java/org/sonar/server/search/Index.java index b81427f9b0c..bb29abb781b 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/Index.java +++ b/sonar-server/src/main/java/org/sonar/server/search/Index.java @@ -19,10 +19,10 @@ */ package org.sonar.server.search; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.picocontainer.Startable; import javax.annotation.CheckForNull; + import java.io.Serializable; public interface Index extends Startable { @@ -38,8 +38,6 @@ public interface Index extends Startable { void delete(K key); - XContentBuilder normalize(K key); - Long getLastSynchronization(); void setLastSynchronization(Long time); diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java b/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java index 6680efacd84..abaeac31bc7 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java @@ -22,6 +22,7 @@ package org.sonar.server.search; import com.google.common.collect.ImmutableList; import org.sonar.server.cluster.LocalQueueWorker; import org.sonar.server.rule2.RuleIndex; +import org.sonar.server.rule2.RuleNormalizer; import java.util.List; @@ -31,6 +32,7 @@ public final class IndexUtils { @SuppressWarnings("unchecked") public static List getIndexClasses() { return ImmutableList.of( + RuleNormalizer.class, RuleIndex.class, LocalQueueWorker.class ); diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java index 6f721af1051..cff60333d7f 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java @@ -105,7 +105,7 @@ public class RuleMediumTest { dao.insert(dto); Hit hit = index.getByKey(dto.getKey()); - assertThat(hit.getFields().get("ruleKey")).isEqualTo(dto.getRuleKey()); + assertThat(hit.getFields().get(RuleNormalizer.RuleFields.RULE_KEY.key())).isEqualTo(dto.getRuleKey().toString()); } @Test