View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.webadmin.console;
21  
22  import groovy.lang.Binding;
23  import groovy.lang.GroovyRuntimeException;
24  
25  import java.io.BufferedOutputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.PrintStream;
28  import java.io.StringWriter;
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.codehaus.groovy.tools.shell.IO;
35  import org.neo4j.server.database.Database;
36  import org.neo4j.server.database.DatabaseBlockedException;
37  
38  import com.tinkerpop.blueprints.pgm.TransactionalGraph;
39  import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph;
40  
41  public class GremlinSession implements ScriptSession
42  {
43  
44      private static final String INIT_FUNCTION = "init()";
45  
46      protected GremlinWebConsole scriptEngine;
47      protected StringWriter outputWriter;
48      private final Database database;
49      private final IO io;
50      private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
51      private final List<String> initialBindings;
52  
53      public GremlinSession( Database database )
54      {
55          this.database = database;
56          PrintStream out = new PrintStream(new BufferedOutputStream( baos ));
57  
58          io = new IO( System.in, out, out);
59  
60          Map<String, Object> bindings = new HashMap<String, Object>();
61          bindings.put( "g", getGremlinWrappedGraph() );
62          bindings.put( "out", out );
63  
64          initialBindings = new ArrayList<String>(bindings.keySet());
65  
66          try
67          {
68              scriptEngine = new GremlinWebConsole( new Binding( bindings ), io );
69          }
70          catch ( final Exception failure )
71          {
72              scriptEngine = new GremlinWebConsole()
73              {
74                  @Override
75                  public void execute( String script )
76                  {
77                      io.out.println( "Could not start Groovy during Gremlin initialization, reason:" );
78                      failure.printStackTrace( io.out );
79                  }
80              };
81          }
82      }
83  
84      /**
85       * Take some gremlin script, evaluate it in the context of this gremlin
86       * session, and return the result.
87       *
88       * @param script
89       * @return the return string of the evaluation result, or the exception message.
90       */
91      @Override
92      public String evaluate( String script )
93      {
94          try {
95  
96              if( script.equals( INIT_FUNCTION )) {
97                  return init();
98              }
99  
100             scriptEngine.execute( script );
101             String result = baos.toString();
102             resetIO();
103             return result;
104         } catch(GroovyRuntimeException ex) {
105             return ex.getMessage();
106         }
107 
108     }
109 
110     private String init() {
111         StringBuffer out = new StringBuffer();
112         out.append("\n");
113         out.append("         \\,,,/\n");
114         out.append("         (o o)\n");
115         out.append("-----oOOo-(_)-oOOo-----\n");
116         out.append("\n");
117 
118         out.append("Available variables:\n");
119         for(String variable : initialBindings) {
120             out.append("  " + variable + "\t= ");
121             out.append(evaluate(variable));
122         }
123         out.append("\n");
124 
125         return out.toString();
126     }
127 
128     private void resetIO()
129     {
130         baos.reset();
131     }
132 
133     private TransactionalGraph getGremlinWrappedGraph()
134             throws DatabaseBlockedException
135     {
136         return new Neo4jGraph( database.graph );
137     }
138 }