|
55 | 55 | import org.cloudfoundry.client.lib.archive.ApplicationArchive;
|
56 | 56 | import org.cloudfoundry.client.lib.archive.DirectoryApplicationArchive;
|
57 | 57 | import org.cloudfoundry.client.lib.archive.ZipApplicationArchive;
|
| 58 | +import org.cloudfoundry.client.lib.domain.ApplicationLog; |
58 | 59 | import org.cloudfoundry.client.lib.domain.ApplicationStats;
|
59 | 60 | import org.cloudfoundry.client.lib.domain.CloudApplication;
|
60 | 61 | import org.cloudfoundry.client.lib.domain.CloudDomain;
|
@@ -222,15 +223,24 @@ public Map<String, String> getLogs(String appName) {
|
222 | 223 | String instance = String.valueOf(0);
|
223 | 224 | return doGetLogs(urlPath, appName, instance);
|
224 | 225 | }
|
225 |
| - |
226 |
| - public StreamingLogToken streamRecentLogs(String appName, ApplicationLogListener listener) { |
227 |
| - return streamLoggregatorLogs(appName, listener, true); |
| 226 | + |
| 227 | + public List<ApplicationLog> getRecentLogs(String appName) { |
| 228 | + AccumulatingApplicationLogListener listener = new AccumulatingApplicationLogListener(); |
| 229 | + streamLoggregatorLogs(appName, listener, true); |
| 230 | + synchronized (listener) { |
| 231 | + try { |
| 232 | + listener.wait(); |
| 233 | + } catch (InterruptedException e) { |
| 234 | + // return any captured logs |
| 235 | + } |
| 236 | + } |
| 237 | + return listener.getLogs(); |
| 238 | + } |
| 239 | + |
| 240 | + public StreamingLogToken streamLogs(String appName, ApplicationLogListener listener) { |
| 241 | + return streamLoggregatorLogs(appName, listener, false); |
228 | 242 | }
|
229 | 243 |
|
230 |
| - public StreamingLogToken streamLogs(String appName, ApplicationLogListener listener) { |
231 |
| - return streamLoggregatorLogs(appName, listener, false); |
232 |
| - } |
233 |
| - |
234 | 244 | public Map<String, String> getCrashLogs(String appName) {
|
235 | 245 | String urlPath = getFileUrlPath();
|
236 | 246 | CrashesInfo crashes = getCrashes(appName);
|
@@ -1613,31 +1623,56 @@ private UUID getAppId(String appName) {
|
1613 | 1623 | return guid;
|
1614 | 1624 | }
|
1615 | 1625 |
|
1616 |
| - private StreamingLogToken streamLoggregatorLogs(String appName, ApplicationLogListener listener, boolean recent) { |
1617 |
| - ClientEndpointConfig.Configurator configurator = new ClientEndpointConfig.Configurator() { |
1618 |
| - public void beforeRequest(Map<String,List<String>> headers) { |
1619 |
| - if (token != null) { |
1620 |
| - headers.put(AUTHORIZATION_HEADER_KEY, Arrays.asList(getAuthorizationHeader())); |
1621 |
| - } |
1622 |
| - } |
1623 |
| - }; |
1624 |
| - |
1625 |
| - UUID appId = getAppId(appName); |
1626 |
| - CloudInfo cloudInfo = getInfo(); |
1627 |
| - String mode = recent ? "dump" : "tail"; |
1628 |
| - URI loggregatorUri = loggregatorUriTemplate.expand(cloudInfo.getLoggregatorEndpoint(), mode, appId); |
1629 |
| - try { |
1630 |
| - WebSocketContainer container = ContainerProvider.getWebSocketContainer(); |
1631 |
| - ClientEndpointConfig config = ClientEndpointConfig.Builder.create().configurator(configurator).build(); |
1632 |
| - Session session = container.connectToServer(new LoggregatorEndpoint(listener), config, loggregatorUri); |
1633 |
| - return new StreamingLogTokenImpl(session); |
1634 |
| - } catch (DeploymentException e) { |
1635 |
| - throw new CloudOperationException(e); |
1636 |
| - } catch (IOException e) { |
1637 |
| - throw new CloudOperationException(e); |
1638 |
| - } |
1639 |
| - } |
1640 |
| - |
| 1626 | + private StreamingLogToken streamLoggregatorLogs(String appName, ApplicationLogListener listener, boolean recent) { |
| 1627 | + ClientEndpointConfig.Configurator configurator = new ClientEndpointConfig.Configurator() { |
| 1628 | + public void beforeRequest(Map<String, List<String>> headers) { |
| 1629 | + if (token != null) { |
| 1630 | + headers.put(AUTHORIZATION_HEADER_KEY, Arrays.asList(getAuthorizationHeader())); |
| 1631 | + } |
| 1632 | + } |
| 1633 | + }; |
| 1634 | + |
| 1635 | + UUID appId = getAppId(appName); |
| 1636 | + CloudInfo cloudInfo = getInfo(); |
| 1637 | + String mode = recent ? "dump" : "tail"; |
| 1638 | + URI loggregatorUri = loggregatorUriTemplate.expand(cloudInfo.getLoggregatorEndpoint(), mode, appId); |
| 1639 | + try { |
| 1640 | + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); |
| 1641 | + ClientEndpointConfig config = ClientEndpointConfig.Builder.create().configurator(configurator).build(); |
| 1642 | + Session session = container.connectToServer(new LoggregatorEndpoint(listener), config, loggregatorUri); |
| 1643 | + return new StreamingLogTokenImpl(session); |
| 1644 | + } catch (DeploymentException e) { |
| 1645 | + throw new CloudOperationException(e); |
| 1646 | + } catch (IOException e) { |
| 1647 | + throw new CloudOperationException(e); |
| 1648 | + } |
| 1649 | + } |
| 1650 | + |
| 1651 | + private class AccumulatingApplicationLogListener implements ApplicationLogListener { |
| 1652 | + private List<ApplicationLog> logs = new ArrayList<ApplicationLog>(); |
| 1653 | + |
| 1654 | + public void onMessage(ApplicationLog log) { |
| 1655 | + logs.add(log); |
| 1656 | + } |
| 1657 | + |
| 1658 | + public void onError(Throwable exception) { |
| 1659 | + synchronized (this) { |
| 1660 | + this.notify(); |
| 1661 | + } |
| 1662 | + } |
| 1663 | + |
| 1664 | + public void onComplete() { |
| 1665 | + synchronized (this) { |
| 1666 | + this.notify(); |
| 1667 | + } |
| 1668 | + } |
| 1669 | + |
| 1670 | + public List<ApplicationLog> getLogs() { |
| 1671 | + Collections.sort(logs); |
| 1672 | + return logs; |
| 1673 | + } |
| 1674 | + } |
| 1675 | + |
1641 | 1676 | private Map<String, Object> findApplicationResource(UUID appGuid, boolean fetchServiceInfo) {
|
1642 | 1677 | Map<String, Object> urlVars = new HashMap<String, Object>();
|
1643 | 1678 | String urlPath = "/v2/apps/{app}?inline-relations-depth=1";
|
|
0 commit comments