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:net.oauth.jsontoken.crypto.MagicRsaPublicKey.java

private static PublicKey parseKey(String magicKey) {
    String[] pieces = magicKey.split(Pattern.quote("."));
    if (pieces.length != 3) {
        throw new IllegalStateException("not a valid magic key: " + magicKey);
    }// w w  w  .j  ava  2s . c o  m

    if (!pieces[0].equals("RSA")) {
        throw new IllegalStateException("unkown key type for magic key: " + pieces[0]);
    }

    String modulusString = pieces[1];
    String exponentString = pieces[2];

    byte[] modulusBytes = Base64.decodeBase64(modulusString);
    byte[] exponentBytes = Base64.decodeBase64(exponentString);

    BigInteger modulus = new BigInteger(modulusBytes);
    BigInteger exponent = new BigInteger(exponentBytes);

    RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
    KeyFactory fac;
    try {
        fac = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("RSA key factory missing on platform", e);
    }
    try {
        return fac.generatePublic(spec);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException("bad key in descripor doc: " + magicKey, e);
    }
}

From source file:net.dv8tion.discord.util.GoogleSearch.java

public static List<SearchResult> performSearch(String engineId, String terms, int requiredResultsCount) {
    try {/* w  ww  .j  a  v  a  2 s  .c  o m*/
        if (GOOGLE_API_KEY == null)
            throw new IllegalStateException(
                    "Google API Key is null, Cannot preform google search without a key! Set one in the settings!");
        if (engineId == null || engineId.isEmpty())
            throw new IllegalArgumentException("Google Custom Search Engine id cannot be null or empty!");

        LocalDateTime currentTime = LocalDateTime.now();
        if (currentTime.isAfter(dayStartTime.plusDays(1))) {
            dayStartTime = currentTime;
            currentGoogleUsage = 1;
        } else if (currentGoogleUsage >= 80) {
            throw new IllegalStateException("Google usage has reached the premature security cap of 80");
        }

        terms = terms.replace(" ", "%20");
        String searchUrl = String.format(GOOGLE_URL, engineId, GOOGLE_API_KEY, requiredResultsCount, terms);

        URL searchURL = new URL(searchUrl);
        URLConnection conn = searchURL.openConnection();
        currentGoogleUsage++;
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 " + randomName(10));
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder json = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            json.append(line).append("\n");
        }
        in.close();

        JSONArray jsonResults = new JSONObject(json.toString()).getJSONArray("items");
        List<SearchResult> results = new LinkedList<>();
        for (int i = 0; i < jsonResults.length(); i++) {
            results.add(SearchResult.fromGoogle(jsonResults.getJSONObject(i)));
        }
        return results;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.leanengine.JsonEncode.java

static JSONObject entityListToJson(List<LeanEntity> entityList) throws LeanException {
    JSONObject rootNode = new JSONObject();
    JSONArray resultsArray = new JSONArray();

    for (LeanEntity entity : entityList) {
        resultsArray.put(entityToJson(entity));
    }/*from w w  w.java  2  s.  c  om*/
    try {
        rootNode.put("result", resultsArray);
    } catch (JSONException e) {
        // this should not happen under normal circumstances
        throw new IllegalStateException(e);
    }
    return rootNode;
}

From source file:com.amalto.core.history.accessor.RootAccessor.java

public void set(String value) {
    throw new IllegalStateException("Cannot set value");
}

From source file:com.autentia.common.util.Assert.java

public static void state(boolean expression, String errMsg) {
    if (!expression) {
        throw new IllegalStateException(errMsg);
    }//from  w w w  . j a v  a 2  s.  c  o  m
}

From source file:cz.cvut.portal.kos.utils.PortalRequestAccessor.java

public static PortletRequest getCurrentRequest() {
    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();

    if (attributes instanceof PortletRequestAttributes) {
        return ((PortletRequestAttributes) attributes).getRequest();
    }//  ww w  .jav a 2  s . com
    throw new IllegalStateException("Does not run within a PortletContext");
}

From source file:com.pulsarang.mom.common.ds.Me2dayDataSource.java

public Me2dayDataSource(Cluster clusterInfo, String dataSourceType) {
    DbInfo configuration = clusterInfo.getDbInfo(dataSourceType);
    if (configuration == null) {
        throw new IllegalStateException("Fail to find db configuration. (" + dataSourceType + ")");
    }/*from  w ww.  j  ava  2  s. c  om*/

    super.setDriverClassName("com.mysql.jdbc.Driver");
    super.setUrl(configuration.getConnectionUrl());
    super.setUsername(configuration.Username());
    super.setPassword(configuration.getPassword());
    super.setMaxActive(configuration.getMaxActive());
    super.setMinIdle(configuration.getMinIdle());

    super.setTestOnBorrow(false);
    super.setTestWhileIdle(true);
    super.setValidationQuery("SELECT 1");
    super.setMaxWait(10000);
    super.setPoolPreparedStatements(false);
    super.setMaxOpenPreparedStatements(1024);
    super.setTimeBetweenEvictionRunsMillis(30000);
    super.setNumTestsPerEvictionRun(5);
    super.setMinEvictableIdleTimeMillis(-1);

    StringBuilder sb = new StringBuilder();
    sb.append("=================================================\n");
    sb.append("url: ").append(super.getUrl()).append("\n");
    sb.append("maxActive: ").append(super.getMaxActive()).append("\n");
    sb.append("minIdle: ").append(super.getMinIdle()).append("\n");
    sb.append("testOnBorrow: ").append(super.getTestOnBorrow()).append("\n");
    sb.append("testWhileIdle: ").append(super.getTestWhileIdle()).append("\n");
    sb.append("maxWait: ").append(super.getMaxWait()).append("\n");
    sb.append("poolPreparedStatements: ").append(false).append("\n");
    sb.append("maxOpenPreparedStatements: ").append(super.getMaxOpenPreparedStatements()).append("\n");
    sb.append("=================================================\n");

    LOG.info("Create datasource '{}'\n{}", dataSourceType, sb.toString());
}

From source file:Main.java

private static String consume(URLConnection connection) throws IOException {
    String encoding = getEncoding(connection);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = connection.getInputStream();
    try {//w  w  w  .j  av  a  2  s .c o  m
        in = connection.getInputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
    } finally {
        try {
            in.close();
        } catch (IOException ioe) {
            // continue
        }
    }
    try {
        return new String(out.toByteArray(), encoding);
    } catch (UnsupportedEncodingException uee) {
        try {
            return new String(out.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException uee2) {
            // can't happen
            throw new IllegalStateException(uee2);
        }
    }
}

From source file:Main.java

private static <T> BinaryOperator<T> throwingMerger() {
    return (u, v) -> {
        throw new IllegalStateException(String.format("Duplicate key %s", u));
    };/*from  w  w  w  .  j a v  a 2  s.c om*/
}

From source file:com.dii.ids.application.utils.io.SimpleDiskCache.java

public static synchronized SimpleDiskCache open(File dir, int appVersion, long maxSize) throws IOException {
    if (usedDirs.contains(dir)) {
        throw new IllegalStateException("Cache dir " + dir.getAbsolutePath() + " was used before.");
    }//from  w w  w .j  ava  2  s  .  c om

    usedDirs.add(dir);

    return new SimpleDiskCache(dir, appVersion, maxSize);
}