felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Felix > 6.3. Programmatically connect to the console
Date Thu, 15 Oct 2009 21:07:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=FELIX&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/FELIX/6.3.+Programmatically+connect+to+the+console">6.3.
Programmatically connect to the console</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~gnodet">Guillaume
Nodet</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/FELIX/6.2.+Building+custom+distributions"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/FELIX/6.2.+Building+custom+distributions">6.2.
Building custom distributions</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/FELIX/6.+Advanced+uses"><img border='0' align='middle' src='/confluence/images/icons/up_16.gif'
width='8' height='8'></a></sup><a href="/confluence/display/FELIX/6.+Advanced+uses">6.
Advanced uses</a></td><td width='33%' class='ScrollbarNextName'>&nbsp;<a
href="/confluence/display/FELIX/6.4.+Embedding+Karaf">6.4. Embedding Karaf</a></td><td
class='ScrollbarNextIcon'><a href="/confluence/display/FELIX/6.4.+Embedding+Karaf"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>
<p><a name="6.3.Programmaticallyconnecttotheconsole-top"></a></p>

<h1><a name="6.3.Programmaticallyconnecttotheconsole-6.3.Programmaticallyconnecttotheconsole"></a>6.3.
Programmatically connect to the console</h1>

<p>A connection to Karaf console can also be done programmatically.<br/>
The following code is a simplified version of the code from the client library.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> org.apache.sshd.ClientChannel;
<span class="code-keyword">import</span> org.apache.sshd.ClientSession;
<span class="code-keyword">import</span> org.apache.sshd.SshClient;
<span class="code-keyword">import</span> org.apache.sshd.client.<span class="code-keyword">future</span>.ConnectFuture;

<span class="code-keyword">public</span> class Main {

    <span class="code-keyword">public</span> <span class="code-keyword">static</span>
void main(<span class="code-object">String</span>[] args) <span class="code-keyword">throws</span>
Exception {
        <span class="code-object">String</span> host = <span class="code-quote">"localhost"</span>;
        <span class="code-object">int</span> port = 8101;
        <span class="code-object">String</span> user = <span class="code-quote">"karaf"</span>;
        <span class="code-object">String</span> password = <span class="code-quote">"karaf"</span>;

        SshClient client = <span class="code-keyword">null</span>;
        <span class="code-keyword">try</span> {
            client = SshClient.setUpDefaultClient();
            client.start();
            ConnectFuture <span class="code-keyword">future</span> = client.connect(host,
port);
            <span class="code-keyword">future</span>.await();
            ClientSession session = <span class="code-keyword">future</span>.getSession();
            session.authPassword(user, password);
            ClientChannel channel = session.createChannel(<span class="code-quote">"shell"</span>);
            channel.setIn(<span class="code-object">System</span>.in);
            channel.setOut(<span class="code-object">System</span>.out);
            channel.setErr(<span class="code-object">System</span>.err);
            channel.open();
            channel.waitFor(ClientChannel.CLOSED, 0);
        } <span class="code-keyword">catch</span> (Throwable t) {
            t.printStackTrace();
            <span class="code-object">System</span>.exit(1);
        } <span class="code-keyword">finally</span> {
            <span class="code-keyword">try</span> {
                client.stop();
            } <span class="code-keyword">catch</span> (Throwable t) { }
        }
        <span class="code-object">System</span>.exit(0);
    }

}
</pre>
</div></div>

<p>You can find a more complete example at the <a href="http://svn.apache.org/repos/asf/felix/trunk/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java"
rel="nofollow">following location</a>.</p>



<p><a href="#6.3.Programmaticallyconnecttotheconsole-top">top</a></p>
<style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/FELIX/6.2.+Building+custom+distributions"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/FELIX/6.2.+Building+custom+distributions">6.2.
Building custom distributions</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/FELIX/6.+Advanced+uses"><img border='0' align='middle' src='/confluence/images/icons/up_16.gif'
width='8' height='8'></a></sup><a href="/confluence/display/FELIX/6.+Advanced+uses">6.
Advanced uses</a></td><td width='33%' class='ScrollbarNextName'>&nbsp;<a
href="/confluence/display/FELIX/6.4.+Embedding+Karaf">6.4. Embedding Karaf</a></td><td
class='ScrollbarNextIcon'><a href="/confluence/display/FELIX/6.4.+Embedding+Karaf"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/FELIX/6.3.+Programmatically+connect+to+the+console">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=97838&revisedVersion=6&originalVersion=5">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/FELIX/6.3.+Programmatically+connect+to+the+console?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message