Example usage for java.lang IllegalStateException IllegalStateException

List of usage examples for java.lang IllegalStateException IllegalStateException

Introduction

In this page you can find the example usage for java.lang IllegalStateException IllegalStateException.

Prototype

public IllegalStateException(Throwable cause) 

Source Link

Document

Constructs a new exception with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

/**
 * Returns an XML representation of the provided node.
 *
 * @param node the node to be represented in XML.
 *
 * @return a string containing an XML representation of the
 * provided DOM node.//w w w.  ja v  a  2 s  . c om
 */
public static String nodeToXmlString(Node node) {

    try {

        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(new StringWriter());

        t.transform(source, result);

        return result.getWriter().toString();

    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new IllegalStateException(e);
    }

}

From source file:com.dvlcube.cuber.utils.FileUtils.java

/**
 * @param file/*from w  ww.jav a 2  s  . c om*/
 *            input.
 * @param nLines
 *            desired number of lines.
 * @return the first n lines of text.
 * @since 07/07/2013
 * @author wonka
 */
public static List<String> head(File file, int nLines) {
    if (file == null)
        throw new IllegalStateException("file not initialized");

    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        int i = 0;
        ArrayList<String> lines = new ArrayList<>();
        String line = null;
        while ((line = reader.readLine()) != null && i < nLines) {
            lines.add(line);
            i++;
        }
        return lines;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object[] readSettings(String file) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from   w w w  . j  a  v  a 2  s. c  om*/
        Object[] res = new Object[in.readInt()];
        for (int i = 0; i < res.length; i++) {
            char cl = in.readChar();
            switch (cl) {
            case 'S':
                res[i] = in.readUTF();
                break;
            case 'F':
                res[i] = in.readFloat();
                break;
            case 'D':
                res[i] = in.readDouble();
                break;
            case 'I':
                res[i] = in.readInt();
                break;
            case 'L':
                res[i] = in.readLong();
                break;
            case 'B':
                res[i] = in.readBoolean();
                break;
            case 'Y':
                res[i] = in.readByte();
                break;
            default:
                throw new IllegalStateException("cannot read type " + cl + " from " + file);
            }
        }
        return res;
    } finally {
        in.close();
    }
}

From source file:org.optaconf.service.AbstractClientArquillianTest.java

private static File findPomFile() {
    File file = new File("pom.xml");
    if (!file.exists()) {
        throw new IllegalStateException("The file (" + file + ") does not exist.\n"
                + "This test needs to be run with the working directory " + POM_DIRECTORY_NAME + ".");
    }/*from  w ww .  ja v a 2  s  .  c  o  m*/
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalStateException("Could not get cannonical file for file (" + file + ").", e);
    }
    if (!file.getParentFile().getName().equals(POM_DIRECTORY_NAME)) {
        throw new IllegalStateException("The file (" + file + ") is not correct.\n"
                + "This test needs to be run with the working directory " + POM_DIRECTORY_NAME + ".");
    }
    return file;
}

From source file:com.seajas.search.contender.service.builder.RSSDirectoryBuilder.java

/**
 * Create a UTF-8 stream containing the contents of the directory as a stream.
 *
 * @param directory//from  w  ww .  j a  va 2 s  . c  om
 * @return InputStream
 * @throws IOException
 */
public static InputStream build(final File directory) throws IOException {
    if (!directory.isDirectory())
        throw new IllegalStateException("The given handle is not a directory");

    // Create a simple feed

    SyndFeedImpl feed = new SyndFeedImpl();

    feed.setEntries(new ArrayList<SyndEntry>());

    // Then add the entries to it

    Map<String, Long> files = travelDirectory(directory, new HashMap<String, Long>());

    for (Map.Entry<String, Long> file : files.entrySet()) {
        SyndEntryImpl entry = new SyndEntryImpl();

        entry.setUri("file://" + file.getKey());
        entry.setTitle(file.getKey());
        entry.setPublishedDate(new Date(file.getValue()));

        feed.getEntries().add(entry);
    }

    // And serialize it to an InputStream

    SyndFeedOutput serializer = new SyndFeedOutput();

    try {
        WebFeeds.validate(feed, URI.create("file://" + directory.getAbsolutePath()));

        return IOUtils.toInputStream(serializer.outputString(feed, true));
    } catch (FeedException e) {
        throw new IOException("Unable to serialize stream", e);
    }
}

From source file:org.cleverbus.core.common.contextcall.ReflectionCallUtils.java

/**
 * Invokes target method.// www.j  av a2s.  c o m
 *
 * @param params the parameters of the call
 * @param beanFactory the Spring bean factory
 * @return response
 */
public static Object invokeMethod(ContextCallParams params, BeanFactory beanFactory) {
    // find target service
    Object targetService = beanFactory.getBean(params.getTargetType());

    // determine method's argument types
    List<Class> argTypes = new ArrayList<Class>();
    for (Object arg : params.getMethodArgs()) {
        argTypes.add(arg.getClass());
    }

    // exist method?
    Method method = ReflectionUtils.findMethod(params.getTargetType(), params.getMethodName(),
            argTypes.toArray(new Class[] {}));
    if (method == null) {
        throw new IllegalStateException("there is no method '" + params.getMethodName() + "' on target type '"
                + params.getTargetType().getSimpleName() + "'");
    }

    // invoke method
    return ReflectionUtils.invokeMethod(method, targetService, params.getMethodArgs().toArray());
}

From source file:configuration.SkipItemWriter.java

@Override
public void write(List items) throws Exception {
    if (failCount < 2) {
        failCount++;/*from   www.  j a v  a  2 s. co  m*/
        throw new IllegalStateException("Writer FOOBAR");
    }
    for (Object item : items) {
        System.out.println(">> " + item);
    }
}

From source file:net.oneandone.sushi.fs.webdav.WebdavFilesystem.java

public static void wireLog(String file) {
    Handler handler;//from  w ww .j  ava  2s.com

    WIRE.setLevel(Level.FINE);
    try {
        handler = new FileHandler(file, false);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    handler.setFormatter(new Formatter() {
        @Override
        public String format(LogRecord record) {
            String message;
            Throwable e;
            StringBuilder result;

            message = record.getMessage();
            result = new StringBuilder(message.length() + 1);
            result.append(message);
            result.append('\n');
            e = record.getThrown();
            if (e != null) {
                // TODO getStacktrace(e, result);
            }
            return result.toString();
        }
    });

    WIRE.addHandler(handler);
}

From source file:Main.java

public static Element getChildElement(Element parent, String tagName, boolean required) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    if (nodes == null || nodes.getLength() == 0) {
        if (required) {
            throw new IllegalStateException(String.format("Missing tag %s in element %s.", tagName, parent));
        } else {//  w  w w . j av a 2 s .c  o m
            return null;
        }
    }
    return (Element) nodes.item(0);
}

From source file:org.seedstack.spring.batch.sample.mapper.UserFieldSetMapper.java

@Override
public User mapFieldSet(final FieldSet fieldSet) throws BindException {
    if (fieldSet == null) {
        throw new IllegalStateException("Exception in Field Set Mapper. Field Set Mapper must not be null...");
    }//from   w  ww.ja  va  2s.com

    final User user = new User();
    user.setFirstName(fieldSet.readString(FIRST_NAME));
    user.setLastName(fieldSet.readString(LAST_NAME));
    user.setEmail(fieldSet.readString(EMAIL));

    return user;
}