MCPcopy Create free account
hub / github.com/cedar-policy/cedar-java / preparseSchemaJni

Function preparseSchemaJni

CedarJavaFFI/src/interface.rs:214–276  ·  view source on GitHub ↗
(
    mut env: JNIEnv<'_>,
    _class: JClass<'_>,
    j_id: JString<'_>,
    j_schema: JString<'_>,
    is_cedar: u8,
)

Source from the content-addressed store, hash-verified

212/// Direct JNI entry point to pre-parse and cache a schema.
213#[jni_fn("com.cedarpolicy.model.schema.Schema")]
214pub fn preparseSchemaJni(
215 mut env: JNIEnv<'_>,
216 _class: JClass<'_>,
217 j_id: JString<'_>,
218 j_schema: JString<'_>,
219 is_cedar: u8,
220) {
221 let id: String = match env.get_string(&j_id) {
222 Ok(s) => s.into(),
223 Err(e) => {
224 let _ = env.throw_new(
225 "com/cedarpolicy/model/exception/InternalException",
226 format!("Failed to read schema cache ID: {e}"),
227 );
228 return;
229 }
230 };
231 let schema_str: String = match env.get_string(&j_schema) {
232 Ok(s) => s.into(),
233 Err(e) => {
234 let _ = env.throw_new(
235 "com/cedarpolicy/model/exception/InternalException",
236 format!("Failed to read schema value: {e}"),
237 );
238 return;
239 }
240 };
241 let parse_result: std::result::Result<Schema, miette::Report> = if is_cedar != 0 {
242 Schema::from_cedarschema_str(&schema_str)
243 .map(|(schema, _warnings)| schema)
244 .map_err(|e| miette::Report::new(e))
245 } else {
246 let json_val: Value = match serde_json::from_str(&schema_str) {
247 Ok(v) => v,
248 Err(e) => {
249 let _ = env.throw_new(
250 "com/cedarpolicy/model/exception/InternalException",
251 format!("Failed to parse schema JSON: {e}"),
252 );
253 return;
254 }
255 };
256 Schema::from_json_value(json_val).map_err(|e| miette::Report::new(e))
257 };
258 match parse_result {
259 Ok(parsed) => {
260 if is_cache_full(&CACHED_SCHEMAS, MAX_CACHED_SCHEMAS.load(Ordering::Relaxed)) {
261 let _ = env.throw_new(
262 "com/cedarpolicy/model/exception/InternalException",
263 "Schema cache is full; increase cedar.cache.maxSchemas or clear unused entries",
264 );
265 return;
266 }
267 CACHED_SCHEMAS.insert(id, parsed);
268 }
269 Err(error) => {
270 let _ = env.throw_new(
271 "com/cedarpolicy/model/exception/InternalException",

Callers

nothing calls this directly

Calls 1

is_cache_fullFunction · 0.85

Tested by

no test coverage detected