/*
 * Copyright (c) 2013 EMBL - European Bioinformatics Institute
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package uk.ac.ebi.fgpt.lode.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ebi.fgpt.lode.utils.DatasourceProvider;
import virtuoso.jdbc4.VirtuosoDataSource;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author Simon Jupp
 * @date 19/07/2013 Functional Genomics Group EMBL-EBI
 */
public class VirtuosoDatasourceProvider implements DatasourceProvider {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private VirtuosoDataSource virtuosoSource = null;

    public VirtuosoDatasourceProvider() {
        // Get the DataSource using JNDI
        if (virtuosoSource == null) {
            try {
                Context context = (Context) (new InitialContext()).lookup("java:comp/env");
                virtuosoSource = (VirtuosoDataSource) context.lookup("jdbc/virtuoso");

            }
            catch (NamingException e) {
                throw new IllegalStateException("Virtuoso JNDI datasource not configured: " + e.getMessage());
            }
        }
    }

    public VirtuosoDatasourceProvider(String endpointUrl, int port) {

        // Get the DataSource
        if (virtuosoSource == null) {
            try {
                Context context = (Context) (new InitialContext()).lookup("java:comp/env");
                virtuosoSource = (VirtuosoDataSource) context.lookup("jdbc/virtuoso");

                virtuosoSource.setServerName(endpointUrl);
                virtuosoSource.setPortNumber(port);
            }
            catch (NamingException e) {
                throw new IllegalStateException("Virtuoso JNDI datasource not configured: " + e.getMessage());
            }
        }
    }

    public DataSource getDataSource() throws SQLException {
        return virtuosoSource;
    }
}
@@ -1192,24 +1192,16 @@
 private 
-Virtuoso
 DataSour
@@ -1203,24 +1203,20 @@
 aSource 
-virtuoso
+data
 Source =
@@ -1314,32 +1314,28 @@
         if (
-virtuoso
+data
 Source == nu
@@ -1462,32 +1462,28 @@
             
-virtuoso
+data
 Source = (Vi
@@ -1476,32 +1476,24 @@
 taSource = (
-Virtuoso
 DataSource) 
@@ -1517,33 +1517,32 @@
 dbc/virtuoso%22);%0A
-%0A
             %7D%0A  
@@ -1834,24 +1834,20 @@
     if (
-virtuoso
+data
 Source =
@@ -1974,32 +1974,152 @@
                 
+// NOTE: If we need setServerName() and setPortNumber(), we insist on the real thing%0A                VirtuosoDataSource 
 virtuosoSource =
@@ -2173,17 +2173,16 @@
 uoso%22);%0A
-%0A
         
@@ -2284,16 +2284,61 @@
 (port);%0A
+                dataSource = virtuosoSource;%0A
         
@@ -2600,24 +2600,20 @@
  return 
-virtuoso
+data
 Source;%0A
@@ -2618,9 +2618,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

Support JavaMelody in app using lode-virtuoso-impl