perfsonar-dev - [pS-dev] [GEANT/SA2/ps-java-services] r5824 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType/storage/sql
Subject: perfsonar development work
List archive
[pS-dev] [GEANT/SA2/ps-java-services] r5824 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType/storage/sql
Chronological Thread
- From:
- To:
- Subject: [pS-dev] [GEANT/SA2/ps-java-services] r5824 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType/storage/sql
- Date: Wed, 31 Aug 2011 11:07:14 +0100
Author: psnc.pikusa
Date: 2011-08-31 11:07:14 +0100 (Wed, 31 Aug 2011)
New Revision: 5824
Added:
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/storage/sql/MysqlMetadataStorageDAOImpl.java
Log:
MySQL Storage DAO implementation
Added:
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/storage/sql/MysqlMetadataStorageDAOImpl.java
===================================================================
---
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/storage/sql/MysqlMetadataStorageDAOImpl.java
(rev 0)
+++
trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsonar/service/measurementArchive/rrdType/storage/sql/MysqlMetadataStorageDAOImpl.java
2011-08-31 10:07:14 UTC (rev 5824)
@@ -0,0 +1,185 @@
+package org.perfsonar.service.measurementArchive.rrdType.storage.sql;
+
+import java.net.URI;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import net.geant.perfsonar.persistence.HibernateConfiguration;
+import net.geant.perfsonar.persistence.Persistence;
+
+import org.perfsonar.base2.service.configuration.Component;
+import org.perfsonar.base2.service.configuration.ConfigurationManager;
+import org.perfsonar.base2.service.exceptions.PerfSONARException;
+import org.perfsonar.base2.xml.Element;
+import org.perfsonar.base2.xml.nmwg.Message;
+import org.perfsonar.base2.xml.nmwg.Metadata;
+import
org.perfsonar.service.measurementArchive.rrdType.storage.MetadataStorageDAO;
+import
org.perfsonar.service.measurementArchive.rrdType.storage.MetadataStorageDAOFactory;
+import
org.perfsonar.service.measurementArchive.rrdType.storage.persistence.MetadaKeyRequestResponse;
+import
org.perfsonar.service.measurementArchive.rrdType.storage.persistence.entity.Interfaces;
+import
org.perfsonar.service.measurementArchive.rrdType.storage.sql.query.QueryGenerator;
+
+import com.mysql.jdbc.Driver;
+
+public class MysqlMetadataStorageDAOImpl extends Component implements
MetadataStorageDAO{
+
+ //private static final String DB_URI =
"jdbc:mysql://localhost:3306/tests2";
+ //private static final String USERNAME = "root";
+ //private static final String PASSWORD = "@jawpcss$";
+
+ protected EntityManager em;
+
+ @Override
+ public void destroy() throws PerfSONARException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void init() throws PerfSONARException {
+
+ String DB_URI = ConfigurationManager.getInstance().
+
getConfiguration().getAuxiliaryComponent(MetadataStorageDAOFactory.METADATA_STORAGE_DAO_NAME).getOption("uri").getValue();
+ String SCHEMA = (String)ConfigurationManager.getInstance().
+
getConfiguration().getAuxiliaryComponent(MetadataStorageDAOFactory.METADATA_STORAGE_DAO_NAME).getOption("schema").getValue();
+ String USERNAME = (String)ConfigurationManager.getInstance().
+
getConfiguration().getAuxiliaryComponent(MetadataStorageDAOFactory.METADATA_STORAGE_DAO_NAME).getOption("username").getValue();
+ String PASSWORD = (String)ConfigurationManager.getInstance().
+
getConfiguration().getAuxiliaryComponent(MetadataStorageDAOFactory.METADATA_STORAGE_DAO_NAME).getOption("password").getValue();
+
+ if(!DB_URI.endsWith("/"))
+ DB_URI = DB_URI + "/";
+
+ try{
+ final HibernateConfiguration config = new
HibernateConfiguration();
+ config.setDbUri(new URI(DB_URI+SCHEMA));
+ config.setDriver(new Driver());
+ config.setDDL("create");
+ config.addEntity(Interfaces.class);
+ final Persistence p = new Persistence(config,
USERNAME, PASSWORD);
+ em = p.newEntityManager();
+ }
+ //TODO create correct perfsonarException
+ catch(Exception e){
+ throw new PerfSONARException("Init Hibernate
connection to connection MySQL failed", e.getMessage());
+ }
+
+ }
+
+ @Override
+ public void run() throws PerfSONARException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public Collection<Element> fetch(Element request) throws
PerfSONARException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ *
+ * @return String with the response message (in NMWG format) of
MetadataKeyRequest
+ * @throws PerfSONARException
+ */
+ public String rawFetch() throws PerfSONARException{
+
+
+ List<Interfaces> resultList =
(List<Interfaces>)em.createNamedQuery(Interfaces.ALL).getResultList();
+ MetadaKeyRequestResponse mKRR = new MetadaKeyRequestResponse();
+
+ return mKRR.fetchMetadataMessage(resultList.iterator());
+ }
+
+ @Override
+ public String rawFetch(Element request) throws PerfSONARException {
+ long t1 = System.currentTimeMillis();
+
+ checkMetadataCorectness((Message)request);
+
+ Map<String, String> parametersMap = new HashMap<String,
String>();
+ QueryGenerator qg = new QueryGenerator();
+
+ parametersMap =
qg.getQuery((Metadata)(((Message)request).getMetadataCollection().iterator().next()),
Interfaces.TABLE);
+ logger.info("QUERY: " + parametersMap.get("query"));
+
+ Query query =
em.createNativeQuery(parametersMap.get("query"), Interfaces.class);
+ for (String parameter : parametersMap.keySet()) {
+ if(!parameter.equals("query"))
+ query.setParameter(parameter,
parametersMap.get(parameter));
+ }
+ MetadaKeyRequestResponse mKRR = new
MetadaKeyRequestResponse();
+
+ String r =
mKRR.fetchMetadataMessage(query.getResultList().iterator());
+
+
+ long t2 = System.currentTimeMillis();
+ logger.info("TIME of rawFetch: " + (t2 - t1));
+
+ return r;
+
+ }
+
+ @Override
+ public void remove(Element request) throws PerfSONARException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void store(Element request) throws PerfSONARException {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void update(Element request) throws PerfSONARException {
+ // TODO Auto-generated method stub
+
+ }
+
+
+ private void checkMetadataCorectness(Message request) throws
PerfSONARException {
+
+ if (request.getMetadataCollection().size() == 0) {
+ logger.warn("can not find any metadatas with a valid
content");
+ throw new PerfSONARException("query_exception",
+ "can not find any metadatas with a
valid content");
+ }
+
+ String eventType = null;
+ for (Metadata m : request.getMetadataCollection()) {
+
+ try {
+ eventType =
m.getFirstEventType().getEventType().trim();
+ } catch (Exception ex) {
+
+ logger.warn("can not find correct eventType
in metadata element");
+ throw new
PerfSONARException("query_exception",
+ "can not find correct
eventType in metadata element");
+ }
+ if (eventType != null)
+ break;
+
+ }
+
+
+ if (eventType == null || eventType.equals("")) {
+ logger.warn("can not find correct eventType in
metadata element");
+ throw new PerfSONARException("query_exception",
+ "can not find eventType in metadata
element");
+ }
+
+
+ }
+
+
+
+
+}
- [pS-dev] [GEANT/SA2/ps-java-services] r5824 - trunk/perfsonar-java-rrd-sql-ma/src/main/java/org/perfsona r/service/measurementArchive/rrdType/storage/sql, svn-noreply, 08/31/2011
Archive powered by MHonArc 2.6.16.