View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2008-2019 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  
20  package ubic.basecode.util.r;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   * Get a connection to R, somehow (if possible).
27   * 
28   * @author Paul
29   * 
30   */
31  public class RConnectionFactory {
32  
33      private static Logger log = LoggerFactory.getLogger( RConnectionFactory.class );
34  
35      /**
36       * Get connection; if Rserve is used, connect to localhost.
37       * 
38       * @return
39       */
40      public static RClient getRConnection() {
41          return getRConnection( "localhost" );
42      }
43  
44      /**
45       * @param hostName The host to use for rserve connections, used only for RServe
46       * @return
47       */
48      public static RClient getRConnection( String hostName ) {
49  
50          RClient rc = null;
51  
52          try {
53              rc = new RServeClient( hostName );
54          } catch ( Exception e ) {
55              // OK, just that RServe is not available.
56          }
57  
58          if ( rc == null ) {
59              rc = getJRIClient();
60              if ( rc != null ) {
61                  return rc;
62              }
63          }
64  
65          return rc;
66      }
67  
68      /**
69       * @return
70       */
71      private static RClient getJRIClient() {
72          RClient j = new JRIClient();
73          log.debug( "Got JRI connection" );
74          return j;
75      }
76  
77  }