Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 53 additions & 20 deletions gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.CharBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -106,11 +107,19 @@ public Object gosh(final CommandSession session, String[] argv) throws Exception
URI uri = baseURI.resolve("etc/gosh_profile");
if (!new File(uri).exists())
{
uri = getClass().getResource("/gosh_profile").toURI();
}
if (uri != null)
{
source(session, uri.toString());
URL url = context.getBundle().getResource("ext/gosh_profile");

if (url != null){
sourceContributed(session, url);
}
else
{
uri = getClass().getResource("/gosh_profile").toURI();
if (uri != null)
{
source(session, uri.toString());
}
}
}
}

Expand Down Expand Up @@ -201,6 +210,20 @@ public Object source(CommandSession session, String script) throws Exception
}
}

public Object sourceContributed(CommandSession session, URL script) throws Exception
{
URI uri = cwd(session).resolve(script.toURI());
session.put("0", uri);
try
{
return session.execute(readScript(script));
}
finally
{
session.put("0", null); // API doesn't support remove
}
}

private Object console(CommandSession session)
{
Console console = new Console(session, history);
Expand All @@ -211,24 +234,34 @@ private Object console(CommandSession session)
private CharSequence readScript(URI script) throws Exception
{
URLConnection conn = script.toURL().openConnection();
int length = conn.getContentLength();

if (length == -1)
{
System.err.println("eek! unknown Contentlength for: " + script);
length = 10240;
}

InputStream in = conn.getInputStream();
CharBuffer cbuf = CharBuffer.allocate(length);
Reader reader = new InputStreamReader(in);
reader.read(cbuf);
in.close();
cbuf.rewind();
return readScript(conn, script.toString());
}

return cbuf;
private CharSequence readScript(URL script) throws Exception
{
URLConnection conn = script.openConnection();
return readScript(conn, script.toString());
}

private CharSequence readScript(URLConnection conn, String script) throws Exception
{
int length = conn.getContentLength();
if (length == -1)
{
System.err.println("eek! unknown Contentlength for: " + script);
length = 10240;
}

InputStream in = conn.getInputStream();
CharBuffer cbuf = CharBuffer.allocate(length);
Reader reader = new InputStreamReader(in);
reader.read(cbuf);
in.close();
cbuf.rewind();

return cbuf;
}

@SuppressWarnings("unchecked")
static Set<String> getVariables(CommandSession session)
{
Expand Down