blob: 636a4e9b6ec561f4d7a188b60a2620ae31184f9f [file] [log] [blame]
Felix Meschbergerefb2d082008-08-19 13:18:47 +00001/*
2* Licensed to the Apache Software Foundation (ASF) under one or more
3* contributor license agreements. See the NOTICE file distributed with
4* this work for additional information regarding copyright ownership.
5* The ASF licenses this file to You under the Apache License, Version 2.0
6* (the "License"); you may not use this file except in compliance with
7* the License. You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*/
17package org.apache.felix.shell.remote;
18
19
Felix Meschbergerefb2d082008-08-19 13:18:47 +000020import java.io.BufferedReader;
21import java.io.IOException;
22import java.io.PrintStream;
23import java.net.Socket;
24
Felix Meschbergerd7c324d2008-08-19 14:10:48 +000025import org.apache.felix.shell.ShellService;
26
Felix Meschbergerefb2d082008-08-19 13:18:47 +000027
28/**
29 * Implements the shell.
Felix Meschbergerefb2d082008-08-19 13:18:47 +000030 */
31class Shell implements Runnable
32{
33
34 private Socket m_Socket;
35 private AtomicInteger m_UseCounter;
36
37
38 public Shell( Socket s, AtomicInteger counter )
39 {
40 m_Socket = s;
41 m_UseCounter = counter;
42 }//constructor
43
44
45 /**
46 * Runs the shell.
47 */
48 public void run()
49 {
50 try
51 {
52 PrintStream out = new TerminalPrintStream( m_Socket.getOutputStream() );
53 BufferedReader in = new BufferedReader( new TerminalReader( m_Socket.getInputStream(), out ) );
54 ReentrantLock lock = new ReentrantLock();
55
56 // Print welcome banner.
57 out.println();
58 out.println( "Felix Shell Console:" );
59 out.println( "=====================" );
60 out.println( "" );
61
62 do
63 {
64 out.print( "-> " );
65 String line = "";
66 try
67 {
68 line = in.readLine();
69 //make sure to capture end of stream
70 if ( line == null )
71 {
72 exit();
73 return;
74 }
75 }
76 catch ( Exception ex )
77 {
78 exit();
79 return;
80 }
81
82 line = line.trim();
83 if ( line.equalsIgnoreCase( "exit" ) || line.equalsIgnoreCase( "disconnect" ) )
84 {
85 in.close();
86 out.close();
87 exit();
88 return;
89 }
90
91 ShellService shs = Activator.getServices().getFelixShellService( ServiceMediator.NO_WAIT );
92 try
93 {
94 lock.acquire();
95 shs.executeCommand( line, out, out );
96 }
97 catch ( Exception ex )
98 {
99 Activator.getServices().error( "Shell::run()", ex );
100 }
101 finally
102 {
103 lock.release();
104 }
105 }
106 while ( true );
107 }
108 catch ( IOException ex )
109 {
110 Activator.getServices().error( "Shell::run()", ex );
111 }
112 }//run
113
114
115 private void exit()
116 {
117 try
118 {
119 m_Socket.close();
120 }
121 catch ( IOException ex )
122 {
123 Activator.getServices().error( "Shell::exit()", ex );
124 }
125 m_UseCounter.decrement();
126 }//exit
127
128}//class Shell