Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 569340 design a way for an admin to interact with the server #688

Merged
merged 2 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.eclipse.passage.lbc.internal.base.api.FloatingState;
import org.eclipse.passage.lic.internal.equinox.io.FileFromBundle;
import org.eclipse.passage.lic.internal.jetty.JettyHandler;
import org.eclipse.passage.lic.internal.jetty.ui.LicensedJettyActivator;
import org.eclipse.passage.lic.internal.jetty.interaction.LicensedJettyActivator;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

Expand Down
11 changes: 0 additions & 11 deletions bundles/org.eclipse.passage.lic.jetty/.settings/.api_filters

This file was deleted.

2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.lic.jetty/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Require-Bundle: javax.servlet;bundle-version="0.0.0",
org.eclipse.passage.lic.net;bundle-version="1.0.100",
org.eclipse.passage.lic.equinox;bundle-version="1.0.201"
Export-Package: org.eclipse.passage.lic.internal.jetty;x-friends:="org.eclipse.passage.lbc.base,org.eclipse.passage.lbc.jetty,org.eclipse.passage.lac.jetty",
org.eclipse.passage.lic.internal.jetty.ui
org.eclipse.passage.lic.internal.jetty.interaction;x-friends:="org.eclipse.passage.lbc.jetty"
Bundle-ActivationPolicy: lazy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.interaction;

public abstract class Command {

private final Scope scope;
private final String[] names;

public Command(Scope scope, String[] names) {
this.scope = scope;
this.names = names;
}

public final Scope scope() {
return scope;
}

public final String[] commands() {
return names;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.interaction;

import java.util.Hashtable;

import org.eclipse.passage.lic.internal.jetty.JettyServer;
import org.osgi.framework.BundleContext;

final class Commands {

private ServerHandles server;

void register(BundleContext context, JettyServer jetty) {
registerSelfStatus(context);
registerServerHandles(context, jetty);
}

ServerHandles server() {
return server;
}

private void registerSelfStatus(BundleContext context) {
register(context, new LicenseStatus());
}

private void registerServerHandles(BundleContext context, JettyServer jetty) {
this.server = new ServerHandles(jetty);
register(context, server);
}

private void register(BundleContext context, Command command) {
Hashtable<String, Object> props = new Hashtable<>();
props.put("osgi.command.scope", //$NON-NLS-1$
command.scope().id());
props.put("osgi.command.function", //$NON-NLS-1$
command.commands());
context.registerService(command.getClass().getName(), command, props);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2021 ArSysOp
* Copyright (c) 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -10,61 +10,28 @@
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.ui;
package org.eclipse.passage.lic.internal.jetty.interaction;

import java.nio.file.Path;
import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.core.runtime.Platform;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.access.GrantLockAttempt;
import org.eclipse.passage.lic.internal.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.internal.base.diagnostic.DiagnosticExplained;
import org.eclipse.passage.lic.internal.base.logging.Logging;
import org.eclipse.passage.lic.internal.base.restrictions.CertificateWorthAttention;
import org.eclipse.passage.lic.internal.equinox.EquinoxPassage;
import org.eclipse.passage.lic.internal.equinox.LicensedApplication;
import org.eclipse.passage.lic.internal.jetty.JettyHandler;
import org.eclipse.passage.lic.internal.jetty.JettyServer;
import org.eclipse.passage.lic.internal.net.connect.Port;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public abstract class LicensedJettyActivator implements BundleActivator {
final class LicenseProtection {

private final Logger log;
private final JettyServer jetty;
private final Logger log = LogManager.getLogger(getClass());
private Optional<GrantLockAttempt> lock = Optional.empty();

public LicensedJettyActivator() {
configureLogging();
this.log = LogManager.getLogger(getClass());
this.jetty = new JettyServer(this::handler);
}

@Override
public final void start(BundleContext context) throws Exception {
registerCommands(context);
if (checkLicense()) {
jetty.launch(new Port(Platform.getApplicationArgs(), 8090));
}
}

@Override
public final void stop(BundleContext context) throws Exception {
lock.ifPresent(acq -> new EquinoxPassage().releaseLicense(acq));
jetty.terminate();
}

private void configureLogging() {
new Logging(this::logConfig).configure();
}

private boolean checkLicense() {
boolean check() {
lock = acquireLicense();
if (!lock.isPresent()) {
return false;
Expand All @@ -81,6 +48,10 @@ private boolean checkLicense() {
return true;
}

void release() {
lock.ifPresent(acq -> new EquinoxPassage().releaseLicense(acq));
}

private Optional<GrantLockAttempt> acquireLicense() {
return product().flatMap(this::acquireLicense);
}
Expand Down Expand Up @@ -110,12 +81,4 @@ private boolean successful(ServiceInvocationResult<GrantLockAttempt> response) {
return response.data().map(GrantLockAttempt::successful).orElse(Boolean.FALSE);
}

private void registerCommands(BundleContext context) {
// new Commands().register(context, jetty);
}

protected abstract JettyHandler handler();

protected abstract Path logConfig() throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.interaction;

import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.diagnostic.Diagnostic;
import org.eclipse.passage.lic.internal.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.internal.base.diagnostic.DiagnosticExplained;
import org.eclipse.passage.lic.internal.equinox.EquinoxPassageLicenseCoverage;

final class LicenseStatus extends Command {

public LicenseStatus() {
super(new Scope.Self(), new String[] { "status" });//$NON-NLS-1$
}

public void status() {
ServiceInvocationResult<ExaminationCertificate> response = new EquinoxPassageLicenseCoverage().assess();
if (response.data().isPresent()) {
reportCertificate(response.data().get());
}
reportDiagnostic(response.diagnostic());
}

private void reportCertificate(ExaminationCertificate certificate) {
System.out.printf("\n%s\n", new RequirementsLicensingStatusExplained(certificate).get()); //$NON-NLS-1$
}

private void reportDiagnostic(Diagnostic diagnostic) {
System.out.printf("\n%s\n", new DiagnosticExplained(diagnostic).get()); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (c) 2020, 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.interaction;

import java.nio.file.Path;

import org.eclipse.passage.lic.internal.base.logging.Logging;
import org.eclipse.passage.lic.internal.jetty.JettyHandler;
import org.eclipse.passage.lic.internal.jetty.JettyServer;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public abstract class LicensedJettyActivator implements BundleActivator {

private final JettyServer jetty;
private ServerHandles server;

public LicensedJettyActivator() {
configureLogging();
this.jetty = new JettyServer(this::handler);
}

@Override
public final void start(BundleContext context) throws Exception {
registerCommands(context);
server.start();
}

@Override
public final void stop(BundleContext context) throws Exception {
server.stop();
}

private void configureLogging() {
new Logging(this::logConfig).configure();
}

private void registerCommands(BundleContext context) {
Commands commands = new Commands();
commands.register(context, jetty);
this.server = commands.server();
}

protected abstract JettyHandler handler();

protected abstract Path logConfig() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.ui;
package org.eclipse.passage.lic.internal.jetty.interaction;

import org.eclipse.passage.lic.internal.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.internal.base.diagnostic.LicensingStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jetty.interaction;

import java.util.Objects;

public abstract class Scope {

private final String id;

protected Scope(String id) {
Objects.requireNonNull(id, "Scope::id"); //$NON-NLS-1$
this.id = id;
}

public String id() {
return id;
}

@Override
public int hashCode() {
return id.hashCode();
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Scope)) {
return false;
}
return ((Scope) obj).id.equals(id);
}

@Override
public String toString() {
return id;
}

public static final class Self extends Scope {

public Self() {
super("self"); //$NON-NLS-1$
}

}

public static final class Server extends Scope {

public Server() {
super("server"); //$NON-NLS-1$
}

}

public static final class Of extends Scope {

public Of(String id) {
super(id);
}

}

}
Loading