Search in sources :

Example 1 with ServiceException

use of org.apache.openejb.server.ServiceException in project tomee by apache.

the class TomcatLoader method destroy.

/**
 * Destroy system.
 */
public static void destroy() {
    for (final ServerService s : services) {
        try {
            s.stop();
        } catch (final ServiceException ignored) {
        // no-op
        }
    }
    // Stop ServiceManager
    if (manager != null) {
        try {
            manager.stop();
        } catch (final ServiceException e) {
        // no-op
        }
        manager = null;
    }
    // Stop Ejb server
    if (ejbServer != null) {
        try {
            ejbServer.stop();
        } catch (final ServiceException e) {
        // no-op
        }
        ejbServer = null;
    }
    final TomcatWebAppBuilder tomcatWebAppBuilder = (TomcatWebAppBuilder) SystemInstance.get().getComponent(WebAppBuilder.class);
    if (tomcatWebAppBuilder != null) {
        try {
            tomcatWebAppBuilder.stop();
        } catch (final Exception ignored) {
        // no-op
        }
    }
    // Destroy OpenEJB system
    OpenEJB.destroy();
}
Also used : ServiceException(org.apache.openejb.server.ServiceException) ServerService(org.apache.openejb.server.ServerService) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) ServiceException(org.apache.openejb.server.ServiceException)

Example 2 with ServiceException

use of org.apache.openejb.server.ServiceException in project tomee by apache.

the class MulticastDiscoveryAgent method start.

/**
 * start the discovery agent
 *
 * @throws ServiceException
 */
@Override
public void start() throws ServiceException {
    try {
        if (running.compareAndSet(false, true)) {
            final InetAddress inetAddress = InetAddress.getByName(host);
            this.address = new InetSocketAddress(inetAddress, port);
            multicast = new Multicast(tracker);
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : ServiceException(org.apache.openejb.server.ServiceException) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) ServiceException(org.apache.openejb.server.ServiceException)

Example 3 with ServiceException

use of org.apache.openejb.server.ServiceException in project tomee by apache.

the class MulticastPulseAgent method start.

@Override
public void start() throws ServiceException {
    if (!this.running.getAndSet(true)) {
        try {
            this.sockets = getSockets(this.multicast, this.port);
        } catch (final Exception e) {
            throw new ServiceException("Failed to get Multicast sockets", e);
        }
        final CountDownLatch latch = new CountDownLatch(this.sockets.length);
        final String mpg = this.group;
        final boolean isLoopBackOnly = this.loopbackOnly;
        final ExecutorService executorService = getExecutorService();
        final MulticastPulseAgent agent = MulticastPulseAgent.this;
        for (final MulticastSocket socket : this.sockets) {
            final String socketKey;
            try {
                socketKey = socket.getNetworkInterface().toString();
            } catch (final SocketException e) {
                LOG.error("Failed to get network interface name on: " + socket, e);
                continue;
            }
            final Sender sender = new Sender(this, socketKey, socket);
            this.futures.add(executorService.submit(sender));
            this.futures.add(executorService.submit(new Runnable() {

                @Override
                public void run() {
                    final DatagramPacket request = new DatagramPacket(new byte[2048], 2048);
                    latch.countDown();
                    while (agent.running.get()) {
                        try {
                            socket.receive(request);
                            final SocketAddress sa = request.getSocketAddress();
                            if (null != sa) {
                                String req = new String(request.getData(), 0, request.getLength());
                                if (req.startsWith(CLIENT)) {
                                    final int ix = req.indexOf(BADURI);
                                    String badUri = null;
                                    if (ix > 0) {
                                        // The client is notifying of a bad uri
                                        badUri = req.substring(ix).replace(BADURI, "");
                                        req = req.substring(0, ix).replace(CLIENT, "");
                                    } else {
                                        req = (req.replace(CLIENT, ""));
                                    }
                                    // Is this a group or global pulse request
                                    if (mpg.equals(req) || "*".equals(req)) {
                                        // Is there a bad url and is it this agent broadcasting the bad URI?
                                        if (null != badUri) {
                                            if (getHosts(agent.ignore).contains(badUri)) {
                                                final ReentrantLock l = agent.lock;
                                                l.lock();
                                                try {
                                                    // Remove it and rebuild our broadcast packet
                                                    if (agent.ignore.add(badUri)) {
                                                        agent.buildPacket();
                                                        LOG.warning("This server has removed the unreachable host '" + badUri + "' from discovery, you should consider adding" + " this to the 'ignore' property in the multipulse.properties file");
                                                    }
                                                } finally {
                                                    l.unlock();
                                                }
                                            }
                                            agent.fireEvent(URI.create("OpenEJB" + BADURI + badUri), false);
                                        } else {
                                            // Normal client multicast pulse request
                                            final String client = ((InetSocketAddress) sa).getAddress().getHostAddress();
                                            if (isLoopBackOnly && !MulticastPulseAgent.isLocalAddress(client, false)) {
                                                // We only have local services, so make sure the request is from a local source else ignore it
                                                if (LOG.isDebugEnabled()) {
                                                    LOG.debug(String.format("Ignoring remote client %1$s pulse request for group: %2$s - No remote services available", client, req));
                                                }
                                            } else {
                                                // We have received a valid pulse request
                                                if (LOG.isDebugEnabled()) {
                                                    LOG.debug(String.format("Answering client '%1$s' pulse request for group: '%2$s' on '%3$s'", client, req, socketKey));
                                                }
                                                // Renew response pulse
                                                sender.pulseResponse();
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (final Exception e) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("MulticastPulseAgent request error: " + e.getMessage(), e);
                            }
                        }
                    }
                    try {
                        socket.close();
                    } catch (final Throwable e) {
                    // Ignore
                    }
                }
            }));
        }
        try {
            // Give threads a reasonable amount of time to start
            latch.await(5, TimeUnit.SECONDS);
        } catch (final InterruptedException e) {
            this.stop();
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) MulticastSocket(java.net.MulticastSocket) SocketException(java.net.SocketException) CountDownLatch(java.util.concurrent.CountDownLatch) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ServiceException(org.apache.openejb.server.ServiceException) ServiceException(org.apache.openejb.server.ServiceException) DatagramPacket(java.net.DatagramPacket) ExecutorService(java.util.concurrent.ExecutorService) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with ServiceException

use of org.apache.openejb.server.ServiceException in project tomee by apache.

the class HsqlService method init.

@Override
public void init(final Properties p) throws Exception {
    final Properties properties = new Properties();
    for (final Map.Entry<Object, Object> entry : p.entrySet()) {
        // Sometimes the properties object has non string values
        if (!(entry.getKey() instanceof String))
            continue;
        if (!(entry.getValue() instanceof String))
            continue;
        final String property = (String) entry.getKey();
        final String value = (String) entry.getValue();
        if (property.startsWith(sc_key_dbname + ".") || property.startsWith(sc_key_database + ".")) {
            throw new ServiceException("Databases cannot be declared in the hsql.properties.  " + "Instead declare a database connection in the openejb.conf file");
        }
        if ("port".equals(property)) {
            properties.setProperty(sc_key_port, value);
        } else if ("bind".equals(property)) {
            properties.setProperty(sc_key_address, value);
        } else {
            properties.setProperty(property, value);
        }
    }
    properties.setProperty(sc_key_no_system_exit, "true");
    final boolean disabled = Boolean.parseBoolean(properties.getProperty("disabled"));
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    if (!disabled && containerSystem != null) {
        final NamingEnumeration<Binding> bindings;
        try {
            bindings = containerSystem.getJNDIContext().listBindings("openejb/Resource/");
            final Set<String> dbnames = new TreeSet<String>();
            for (final Binding binding : Collections.list(bindings)) {
                final Object value = binding.getObject();
                if (value instanceof DataSource) {
                    final DataSource jdbc = (DataSource) value;
                    Connection connection = null;
                    String path = null;
                    try {
                        connection = jdbc.getConnection();
                        final DatabaseMetaData meta = connection.getMetaData();
                        path = getPath(meta.getDriverName(), meta.getURL());
                    } catch (Throwable t) {
                        continue;
                    } finally {
                        if (connection != null) {
                            try {
                                connection.close();
                            } catch (SQLException sqlEx) {
                            // no-op
                            }
                        }
                    }
                    if (path != null) {
                        if (dbnames.size() > 9) {
                            throw new ServiceException("Hsql Server can only host 10 database instances");
                        }
                        String dbname = path.substring(path.lastIndexOf(':') + 1);
                        dbname = dbname.substring(dbname.lastIndexOf('/') + 1);
                        if (!dbnames.contains(dbname)) {
                            properties.put(sc_key_dbname + "." + dbnames.size(), dbname);
                            properties.put(sc_key_database + "." + dbnames.size(), path);
                            dbnames.add(dbname);
                        }
                    }
                }
            }
        } catch (NameNotFoundException e) {
        // Ignore
        }
        // create the server
        server = new Server();
        // add the silent property
        properties.setProperty(sc_key_silent, "true");
        // set the log and error writers
        server.setLogWriter(new HsqlPrintWriter(false));
        server.setErrWriter(new HsqlPrintWriter(true));
        server.setProperties(new HsqlProperties(properties));
        // get the port
        port = server.getPort();
        // get the Address
        final String ipString = server.getAddress();
        if (ipString != null && ipString.length() > 0) {
            this.ip = ipString;
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) Binding(javax.naming.Binding) Server(org.hsqldb.Server) SQLException(java.sql.SQLException) NameNotFoundException(javax.naming.NameNotFoundException) Connection(java.sql.Connection) Properties(java.util.Properties) HsqlProperties(org.hsqldb.persist.HsqlProperties) HsqlDatabaseProperties(org.hsqldb.persist.HsqlDatabaseProperties) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource) ServiceException(org.apache.openejb.server.ServiceException) TreeSet(java.util.TreeSet) HsqlProperties(org.hsqldb.persist.HsqlProperties) Map(java.util.Map)

Example 5 with ServiceException

use of org.apache.openejb.server.ServiceException in project tomee by apache.

the class MultipointDiscoveryAgent method start.

/**
 * start the discovery agent
 *
 * @throws ServiceException On error
 */
@Override
@Managed
public void start() throws ServiceException {
    try {
        if (running.compareAndSet(false, true)) {
            LOGGER.info("MultipointDiscoveryAgent Starting");
            multipointServer = new MultipointServer(host, discoveryHost, port, tracker, name, debug, roots, reconnectDelay).start();
            LOGGER.info("MultipointDiscoveryAgent Started");
            this.port = multipointServer.getPort();
        }
    } catch (Exception e) {
        throw new ServiceException(port + "", e);
    }
}
Also used : ServiceException(org.apache.openejb.server.ServiceException) IOException(java.io.IOException) ServiceException(org.apache.openejb.server.ServiceException) Managed(org.apache.openejb.monitoring.Managed)

Aggregations

ServiceException (org.apache.openejb.server.ServiceException)7 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)2 DatagramPacket (java.net.DatagramPacket)1 InetAddress (java.net.InetAddress)1 MulticastSocket (java.net.MulticastSocket)1 SocketAddress (java.net.SocketAddress)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Binding (javax.naming.Binding)1